What kind of test cases can we write using DUnit?
Asked Answered
D

2

6

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?

Dauphine answered 23/11, 2012 at 12:14 Comment(3)
You can write UI tests with DUnit, but the support for that is rather limited. However, please don't tell me your application logic exists in OnClick event handlers. If you re-factored your application to separate GUI from application logic, then you can test the application logic directly.Adore
Have a look at Model View ViewModel (MVVM) as one of many possible ways to produce testable code. After understanding that you will get the DUnit pointDutton
For MVVM, you might want to check out the video from CodeRage7 - blogs.embarcadero.com/davidi/2012/11/20/41986Appreciable
T
17

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

Taxeme answered 23/11, 2012 at 12:38 Comment(3)
And for refactoring your code I just stumbled on this intro: tabdee.ltd.uk/papers/IntroductionToRefactoring.htmlTaxeme
Pretty sure you could remove all but one or two links and get it to fit as a comment.Trilingual
I found this interesting article on "DUnit and TCustomAttributes"Dicentra
S
4

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).

Sweatband answered 24/11, 2012 at 3:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.