Question

I am using Delphi 7.

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

Is it possible to write test cases for a particular button click event? Because in that event there may be a big set of code used in which more units are called with their respective database related procedures or functions. In that case, what is the best approach to write the test cases and how? (If possible, an example or referal would be a great help for me).

Because, in sample DUnit project we can't write entire sets of application code, because there may be some other form required to be created in some cases.

So, my doubt is for such situations how to write test cases, and generally also what type of test cases we can write using DUnit and importantly how?

Was it helpful?

Solution

This should be a comment, but I'm writing it as an answer because it won't fit in a comment.

I suggest you do two things:

  • Investigate decoupling your business logic from your interface (refactoring your code)
    Generally you split your code into self-containing units, then unit test the core functionality of a unit "from the bottom up". You don't unit test the GUI. Google e.g. "unit testing decoupling delphi units"

  • Check out the below links for examples for 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 are you doing 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

OTHER TIPS

Learn unit testing by writing simple tests. Simple tests are for a class you are building that is unit-testable. A unit testable class is not a complete Delphi GUI application.

Try this as a first exercise:

  1. Make a class that can factor an integer from 1 to MAXINT into a list of prime numbers that when multiplied together, result in the original number. The results should be sorted smallest factor to largest. If the size of that result list is 1, then the result is prime. Zero and negative numbers raise exceptions.

  2. Instead of making the whole thing at one go, try "red/green" test driven development. That means just write enough code to make the first test fail. (Write the first test, write enough code that the class you're building compiles and runs, but fails the test. Now make the first test pass. Not solve the whole factoring-numbers, just pass the first test.)

  3. Repeat the process of writing failing tests and then the code you need to make it pass until you've provide (with your tests) that your class can now factor any integer value from 1 to MAXINT.

When it comes to "working unit testing into your existing legacy applications", you should check out a book called "Working Effectively with Legacy Code". I can't give you any short introduction to how to go about starting to add unit tests to a big ball of mud (your button OnClick code).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top