Question

I want to write first unit test in my life.

At present, I am developing new ASP.NET MVC 5 project. This is simple workflow system. My project contains 4 layers:

  • Presentaton layer (an MVC-project)
  • Infrastructure layer (which contains Repositories and ORM)
  • Domain (which contains POCO classes and interfaces of busines logic)
  • Service layer (which is implemented domain interfaces)

I believe, that I need test Service layer firstly. Is that right? Which layer should I test first?

Was it helpful?

Solution

There's no single correct approach, but the most common techniques are

  • Top-Down, also called Outside-In. Here, you start at the outside layer and work your way in.
  • Bottom-Up. Here, you start with the constituent building blocks and assembly them to a working system.

As Code Complete describes, using dual approaches interchangeably can actually be beneficial, because the stuff you learn from doing one thing, helps you better understand what you need to do in the other end, and vice versa. I often do a bit of Outside-In, then some Bottom-Up, then some more Outside-In, etc.

OTHER TIPS

As per Mark Seemann's answer, you can test from UI layer first and finish with the data layer, or in reverse.

Who is responsible for the project? Which part of the project is business critical? Rather than test across each layer "horizontally", test through all of the layers for a particular piece of functionality "vertically".

This gives you the benefit of coverage based on business priorities and you can apply any testability changes you need to make or techniques across all of the layers as you start to test each piece of functionality.

Since you have written your code already, be prepared to refactor some code to make it more testable (for example setting up Dependency Injection to isolate code for unit testing) and make note of these changes to help design for testability in future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top