1

I am learning test driven develeopment and read some books about TDD. I learned rules of unit testing. How can I write unit test, how can I select test method names, Act, Assert, Action and like this.

In real word I developed applications that have layered architecture. Domain Layer, Data Layer, Service Layer, UI Layer, ....

How can I start an application with TDD. Is there a rule or way or else. For example starting Service Layer.

1
  • 1
    Recommended reading: Where to start?
    – user22815
    Commented Dec 29, 2014 at 22:56

1 Answer 1

3

The size of a project should make no difference whatsoever when doing test driven development. The point of unit testing is that you are testing isolated units of functionality completely independently of other areas of the code.

This is achieved by using mocks to fake out the expected behavior of other parts of the program. This can be most easily achieved by always coding against interfaces instead of concrete types.

For example, say you have two classes ClassA and ClassB. ClassA calls ClassB, but you want to test ClassA independently as it is a separate unit from ClassB. Instead of directly referencing ClassB from within ClassA, you instead make ClassB implement interface IClassB. Then when writing unit tests for ClassA, you simply mock the behavior of methods on IClassB. You may then proceed to write isolated unit tests for ClassB.

This process can be facilitated with the use of dependency injection which allows the intended concrete type to be decided at application runtime.

Taking this approach, you are only ever testing each individual part of the system in isolation, so it makes no difference whatsoever where you begin. This is pretty much the definition of unit testing.

What is probably more important at the start of the project is to pick a mocking framework and a dependency injection framework to allow you to work this way. There will be a variety available for whichever language you are working in.

The act of testing multiple tiers of the software at the same time is called Integration Testing. These tests cannot reasonably be expected to be fully written before all the parts of the system are at least in place (if not fully).

Not the answer you're looking for? Browse other questions tagged or ask your own question.