What test cases can we write with DUnit?

I am using Delphi 7.

I am new to DUnit, my doubt is which test cases I can write using DUnit and how (which is very important for me).

Can I write test cases for a specific button click event? Because in this case, there may be a large set of code in which more units are called with the corresponding procedures or functions associated with the database. Then what is the best approach for writing test cases and how? (If possible, an example or referral would be a big help for me).

Since in the DUnit project example we cannot write entire sets of application code, because in some cases some other form may be created.

So, I doubt situations like writing test cases, and generally also what types of test cases we can write using DUnit and, what is important, how?

+6
source share
2 answers

This should be a comment, but I am writing it as an answer, because it will not fit into the comment.

I suggest you do two things:

  • Learn the decoupling of your business logic from your interface (refactoring your code)
    As a rule, you divide your code into independent units, then unit test the basic functionality of the device from the bottom up. You are not a unit test GUI. Google for example. "Delphi unit isolation test"

  • Check out the links below for examples of what you can do with unit testing:

http://www.howtodothings.com/computers/a928-automated-testing-with-dunit.html
http://www.nldelphi.com/cgi-bin/articles.exe/ShowArticle?ID=14697
https://lists.sourceforge.net/lists/listinfo/dunit-interest
http://www.delphi-treff.de/tutorials
http://sourceitsoftware.blogspot.com/2008/10/getting-line-numbers-in-dunit-test.html
http://wiert.me/2010/09/08/delphi-use-tstrings-to-parse-non-standard-separated-strings-and-validate-it-with-dunit-tests/
http://delphi.about.com/od/vclusing/a/autotestvcl.htm
http://dunit.sourceforge.net/
http://delphi.about.com/od/toppicks/tp/aatpdebug.htm
http://www.nickhodges.com/post/Delphi-Mocks-The-Basics.aspx
http://www.finalbuilder.com/Resources/Blogs/tabid/458/EntryId/287/Introducing-Delphi-Mocks.aspx
https://github.com/Vsofttechnologies/delphi-mocks
http://www.uweraabe.de/Blog/2012/03/17/a-dunit-folder-iterator-extension/
Unit testing in Delphi - how do you do it?
http://members.optusnet.com.au/~mcnabp/
http://www.nickhodges.com/post/The-Vocabulary-of-Unit-Testing.aspx
http://hanselminutes.com/169/the-art-of-unit-testing-with-roy-osherove

+16
source

Learn unit testing by writing simple tests. Simple tests are for the class you are building, which can be tested individually. A single test class is not a complete Delphi GUI application.

Try this as the first exercise:

  • Create a class that can convert an integer from 1 to MAXINT to a list of primes that, when multiplied together, result in the original number. Results should be sorted with the smallest coefficient to the largest. If the size of this resulting list is 1, the result will be simple. Zero and negative numbers cause exceptions.

  • Instead of thinking it all over, try a red / green test drive. This means that writing enough code is enough for the first test to fail. (Write the first test, write enough code that the class you are building compiles and runs, but the test fails. Now do the first test pass. Do not resolve all factoring numbers, just pass the first test.)

  • Repeat the process of writing unsuccessful tests, and then the code that you need to pass until you provide (with your tests) that your class can now take into account any integer value from 1 to MAXINT.

When it comes to "testing the work block in your existing legacy applications," you should read the book, Effective Work with Legacy Code. I can't give you a brief introduction on how to start adding unit tests to a big ball of dirt (your OnClick code on the button).

+4
source

All Articles