Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start unit-test old and new code?

I admit that I have almost none experience of unittesting. I did a try with DUnit a while ago but gave up because there was so many dependencies between classes in my application. It is a rather big (about 1.5 million source lines) Delphi application and we are a team that maintain it.

The testing for now is done by one person that use it before release and report bugs. I have also set up some GUI-tests in TestComplete 6, but it often fails because of changes in the application.

Bold for Delphi is used as persistance framework against the database. We all agree that unittesting is the way to go and we plan to write a new application in DotNet with ECO as persistance framework.

I just don't know where to start with unittesting... Any good books, URL, best practice etc ?

like image 682
Roland Bengtsson Avatar asked Jul 16 '09 12:07

Roland Bengtsson


People also ask

How do you start a unit test?

To get started, select a method, a type, or a namespace in the code editor in the project you want to test, right-click, and then choose Create Unit Tests. The Create Unit Tests dialog opens where you can configure how you want the tests to be created.

How do you refactor codes in unit testing?

How do I refactor unit tests? Quite simply, the same as you would any other code - in isolation. So, if you want to refactor your tests, don't change the code, just change your tests.


4 Answers

Well, the challenge in unit testing is not the testing itself, but in writing testable code. If the code was written not thinking about testing, then you'll probably have a really hard time.

Anyway, if you can refactor, do refactor to make it testable. Don't mix object creation with logic whenever possible (I don't know delphi, but there might be some dependency injection framework to help in this).

This blog has lots of good insight about testing. Check this article for instance (my first suggestion was based on it).

As for a suggestion, try testing the leaf nodes of your code first, those classes that don't depend on others. They should be easier to test, as they don't require mocks.

like image 187
Samuel Carrijo Avatar answered Nov 15 '22 19:11

Samuel Carrijo


Writing unit tests for legacy code usually requires a lot of refactoring. Excellent book that covers this is Michael Feather's "Working Effectively with Legacy Code"

One additional suggestion: use a unit test coverage tool to indicate your progress in this work. I'm not sure about what the good coverage tools for Delphi code are though. I guess this would be a different question/topic.

Working Effectively with Legacy Code

like image 34
b.roth Avatar answered Nov 15 '22 18:11

b.roth


One of the more popular approaches is to write the unit-tests as you modify the code. All new codes gets unit tests, and for any code you modify you first write its test, verify it, modify it, re-verify it, and then write/fix any tests that you need due to your modifications.

One of the big advantages of having good unit test coverage is being able to verify that the changes you make don't inadvertently break something else. This approach allows you to do that, while focusing your efforts on your immediate needs.

The alternate approach I've employed is to develop my unit tests via Co-Ops :)

like image 26
Chris Arguin Avatar answered Nov 15 '22 18:11

Chris Arguin


When you work with legacy code, mock objetcs are really usefull to build unit tests.

Take a look at this question regarding Delphi and mocks: What is your favorite Delphi mocking library?

like image 30
Pierre-Jean Coudert Avatar answered Nov 15 '22 18:11

Pierre-Jean Coudert