How to set up your test project folder structure
less than a minute read
Here is my take on how test projects should be structured within a solution as well as list of my preferred tools. I have used this in the past and find it makes for easy navigation and understanding.
Framework
The testing framework I most commonly use is NUnit I also prefer to use Moq as my mocking framework.
Project Structure
- Create a test project for each project under test
- Within the test project try and keep the namespaces in sync so that a test is immediately locatable by the class it is testing.
- Create a folder named after the class under test, suffixed with ‘Tests’ (this is more than simply for descriptive purposes, it also saves a lot of headaches with namespaces)
- Create a test file for each method under test, again with a ‘Tests’ suffix.
Below is a diagram which explains the above in a bit more detail.
// Project under test
- Solution.ProjectA.csproj [Project]
- NamespaceA [Folder]
- Namespace1 [Folder]
TestableClass.cs [File]
- Method1()
- Method2()
//Test project
- Solution.ProjectA.Tests.csproj [Test Project]
- NamespaceA [Folder]
- Namespace1 [Folder]
- TestableClassTests [Folder]
- TestableClassTestBase.cs [File] <-- common setup methods and build mocks
- Method1Tests.cs [File]
- Method2Tests.cs [File]
The Tests
In terms of the tests themselves, follow the Arrange, Act, Assert pattern as follows:
[TestMethod]
public void Method_Scenario_ExpectedResult()
{
//Arrange
// The object under test would normally be setup in the [TestInitialize] method
var objectUnderTest = ...
var parameterValue = ...
//Act – perform the action you are testing
Var result = objectUnderTest.Method1(parameterValue);
//Assert
Assert.IsTrue(result);
}
- Methods should be named as the title above describes (Method_Scenario_ExpectedResult)
Method
– Method name under test, this will be the same for each test in a particular file.Scenario
– The scenario under testExpectedResult
– the expected result (null is returned, mock method is called etc etc)- Where possible each test should only have one assertion (although this isn’t always possible in reality)
If you have any questions or pointers to add, please don't hesitate to get in touch with me