views:

176

answers:

3

I would like to know what are your practices when you test your classes.

For example, I like to use inheritance, with my fixtures. Given two classes BaseClass, SubClass, I make two other classes BaseClassFixture and SubClassFixture (SubClassFixture is a sub class of BaseClassFixture). So I'm sure that I don't break code which use SubClass as a BaseClass (And people who extends my class can be sure if they do things right, by creating another sub class of my fixture).

I do fixture inheritance with interfaces too. For example, when I create a fixture for IList, I check that any Add, increase Count by one. When I have a concrete class which implements IList I just create a fixture named MyConcreteClassIListFixture.

In that case, the fixture for my interface is abstract and I let my subclass create the instance for my tests.

I think it's a kind of design by contracts (see Bertrand Meyer), because I check invariant before and after any tests.

I do this especially with published interfaces or classes.

And you... what are your practices ??

+1  A: 

My most important rule is that each test should be atomic and should run in no particular order.

For unit tests, they should strictly obey seperation of concerns. For integration tests, I pay extra attention to make sure they follow the most important rule.

Also, tests should follow the DRY rule as much as possible along with the code.

IAmCodeMonkey
A: 

There are couple of important things when writing unit test.

1) Unit Tests should be independent:

Unit Tests must be independent. This means that your unit test should not depend on external things to run. This includes internet connection, external web services etc.

2) Unit Tests should be fast:

Unit Tests should run fast. You can write unit tests in multiple ways. Some of them include the dataaccess even though you don't need to access data to run the test. You can always use mock objects and mock the data access layer.

3) Good naming convention:

Unit Tests should have good naming convention and should read like stories.

Here is one example:

public class when_user_transfer_money_from_source_account_to_destination_account

public void make_sure_error_is_thrown_when_source_account_has_insufficient_funds() {

}

Here is a good screencast that covers many of the above points:

http://screencastaday.com/ScreenCasts/32_Introduction_to_Mocking.aspx

azamsharp
A: 

I think this has already been discussed here:

http://stackoverflow.com/questions/195856/writing-quality-tests

Sergio Acosta