public void MyTest() {
// Arrange - Only setup code needed by the act step{{nl}}
// Act - Only the action(s) under test{{nl}}
// Assert - Verification of the excepted behavior
}
class SubjectTests {
// fields
int callCount = 0;
// help methods
private Subject MakeSubject() =>
new Subject();
// test methods
public void Test1() { }
public void Test2() { }
public void Test3() { }
}
|
Solitary
Type of test that tests a unit without the involvement of other units. Mocks all dependencies of the subject under test. |
|
Sociable
Type of test that uses multiple units to verify a given behavior. Mock only hard to manage dependencies. (e.g. external resources) |
|
We have control over all it's parts.
|
|
Runs in any order.
|
|
Doesn't depend on another test.
|
|
Doesn't produce side-effects.
|
|
Asserts observable behavior.
|
// Convention #1
public void Creating_a_user_stores_it_in_the_database() { ... }
public void Creating_a_user_without_name_throws_exception() { ... }
// Convention #2
public void CreateUser_StoresInDatabase() { ... }
public void CreateUser_WithoutName_ThrowsException() { ... }
// Convention #3
public void Given_user_when_creating_then_its_stored_in_database() { ... }
public void Given_user_when_has_no_name_then_throws_exception() { ... }
public void Test1() {
for(var x in listOfInt) {
Assert.That(GetValue(x), Is.True));
}
}
|
// Thats how it's done
mock.Verify(...) |
|
// If possible use specific value, is int.MaxValue a valid expectation?
Assert.That(x, Is.GreaterThan(10)) |
|
// Might be null
var result = GetObject();Assert.That(result.Property, Is.True)) |
|
// Might throw exception somewhere other than action
[ExpectException()] |
|
// Look for content not types
Assert.IsInstanceOfType(result, typeof(SomeDataModel)); |
|
Sign
|
Outcome
|
|
A big arrange section: large dto, many parameters or many mocks.
|
Subject under test might be doing to much.
|
|
Tests to data model object (dto).
|
Reveals missing tests. DTOs will get their coverage from usage.
|
|
Tests to Exceptions.
|
Reveals missing tests. Exceptions will be tested by their usage.
|
|
Big test file
|
Can indicate duplication or the subject under test is doing to much.
|
|
Json, xml, etc
|
Formatted strings of any kind reveal coupling. Except tests to formatters.
|
[Test]
public void Test1([Values(1, 2, 3)] int value) { }
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(3)]
public void Test1(int value) { }
[DataTestMethod]
[DataRow(1)]
[DataRow(2)]
[DataRow(3)]
public void Test1(int value) { }
static int value = 0;
public void Test1() {
value = 10;
doSomething(value);
}
public void Test2() {
doSomething(value);
}
static object lockObject = new object();
public void Test1() {
lock(lockObject) {
// thread safe code
}
}
public void Test2() {
lock(lockObject) {
// thread safe code
}
}
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.