- Consider naming fixtures, methods and functions from a users' point-of-view
- Stick to
Arrange-Act-Assert
conventions - Test fixtures should be
Snake_cased
- Test methods should be
snake_cased
- Test functions should be
snake_cased
- Divide test functions with new line
- Test functions should be a single line statement
- Do not use curly brackets for single line test functions
- Fields should appear first in a test fixture
- Do not use visibility modifiers on fields
✔️ Consider:
public class Given_a_car
{
Car subject;
bool is_stopped;
[SetUp]
public void SetUp() =>
subject = new Car();
[LoFu, Test]
public void when_stopped()
{
is_stopped = subject.Stop();
void should_turn_off_the_engine() =>
is_stopped.Should().BeTrue();
}
}
❌ Avoid:
public class CarTests
{
Car car;
bool value;
[SetUp]
public void SetUp() =>
car = new Car();
[LoFu, Test]
public void Stop()
{
value = car.Stop();
// What should be true? The test output will be ambiguous!
void should_be_true() =>
value.Should().BeTrue();
}
}
Test fixture SetUp
is for arrange, test method is for acting, and test functions should be used for asserting.
public class Given_a_car
{
Car subject;
bool is_stopped;
[SetUp]
public void SetUp() =>
subject = new Car(); // arrange
[LoFu, Test]
public void when_stopped()
{
is_stopped = subject.Stop(); // act
void should_turn_off_the_engine() =>
is_stopped.Should().BeTrue(); // assert
}
}
The test fixtures should begin with Given
and be Snake_cased
✔️ Consider:
public class Given_a_car
{
}
❌ Avoid:
public class GivenACar
{
}
The test methods should begin with when
and be snake_cased
✔️ Consider:
[LoFu, Test]
public void when_stopped()
{
}
❌ Avoid:
[LoFu, Test]
public void WhenStopped()
{
}
The test functions should begin with should
and be snake_cased
✔️ Consider:
void should_turn_off_engine() =>
is_stopped.Should().BeTrue();
❌ Avoid:
void ShouldTurnOffEngine() =>
is_stopped.Should().BeTrue();
✔️ Consider:
void should_turn_off_the_engine() =>
is_stopped.Should().BeTrue();
❌ Avoid:
void should_turn_off_the_engine() => is_stopped.Should().BeTrue();
✔️ Consider:
void should_turn_off_engine() =>
is_stopped.Should().BeTrue();
❌ Avoid:
void should_turn_off_engine()
{
var stopped = subject.Stop();
stopped.Should().BeTrue();
}
✔️ Consider:
void should_turn_off_the_engine() =>
is_stopped.Should().BeTrue();
❌ Avoid:
void should_turn_off_the_engine() =>
{
is_stopped.Should().BeTrue();
}
✔️ Consider:
public class Given_a_car
{
bool is_stopped;
[LoFu, Test]
public void when_stopped()
{
is_stopped = subject.Stop();
void should_turn_off_the_engine() =>
is_stopped.Should().BeTrue();
}
}
❌ Avoid:
public class Given_a_car
{
[LoFu, Test]
public void when_stopped()
{
is_stopped = subject.Stop();
void should_turn_off_the_engine() =>
is_stopped.Should().BeTrue();
}
bool is_stopped;
}
✔️ Consider:
public class Given_a_car
{
bool is_stopped;
}
❌ Avoid:
public class Given_a_car
{
private bool is_stopped;
}