A common approach to structure unit tests in .NET is to copy a tested project structure to a project with unit tests, e.g.:
/Solution
|__/Crm.Application
|__/Commands
|__/User
|__CreateUserCommand.cs
|__UpdateUserCommand.cs
|__DeleteUserCommand.cs
|__/Crm.Application.Tests
|__/Commands
|__/User
|__CreateUserCommandTests.cs
|__UpdateUserCommandTests.cs
|__DeleteUserCommandTests.cs
Now you have to:
- Manually create directories that you've already created, but in another parent directory;
- Maintain these directories during refactorings and rename or replace them manually;
- Track stale directories;
- Look at the garbage during MRs.
If nothing from these bother you - it's totally ok, but I'm exhausted with
tons of mkdir and Cmd+C / Cmd+V commands.
Go has a nice approach with unit testing directories structure:
- You create a
tested_filename_test.gofile near atested_file.go; - Write unit tests in
tested_filename_test.goand code intested_file.go; go testrun tests, butgo runandgo buildcompletely ignores them and all test dependencies.
- Disable default compile items;
- Split dependencies in two groups. One for tests and one for a project;
- Add different compile items when you need test dependencies and when you don't.
make runwill run an application;make testwill test it;make publishwill publish it without any test dependency or test code;- And you can maintain one set of directories and create tests near tested code.