Skip to content

Unit Test Organisation

Thomas Klepzig edited this page Sep 16, 2016 · 11 revisions

1. One test-class for one implementation-class with multiple test methods

  • Every class of implementation which should be tested has one corresponding test class
  • This class contains multipe test methods, every method is acting and asserting for its use case
  • aka Testcase class per class
public class CalculatorTests
{
    void Divide_should_return_2_for_10_and_5()
    {
        //arrange
        //act
        //assert
    }
    void Divide_should_throw_exception_by_dividing_by_zero()
    {
        //arrange
        //act
        //assert
    }
}

2. One test-class for one implementation-method and its input parameters

  • There is no exact mapping of an implementation-class to a test-class
  • Instead, the tests are organized and named like requirements
  • One test class is testing only one single method of one
  • aka Testcase class per fixture
public class When_dividing_10_by_5
{
    void arrange()
    {
    }
    void act()
    {
    }
    void It_results_to_2()
    {
    }
}

public class When_dividing_10_by_0
{
    void arrange()
    {
    }
    void act()
    {
    }
    void It_throws_an_exception()
    {
    }
}

3. Mix of both

  • Name the test-class like a requirement, as mentioned in 2.
  • Make multiple acts and assertions (and so mutiple test methods) for different input parameters for the one method you want to test

http://xunitpatterns.com/Testcase%20Class%20per%20Fixture.html

Clone this wiki locally