Show Menu

C# Unit-Tests - Basics Cheat Sheet by

This my description

Arrange - Act - Assert

public void MyTest() {
  // Arrange - Only setup code needed by the act step
  // Act - Only the action(s) under test
  // Assert - Verification of the excepted behavior }
Tip: If any of these parts are greatly bigger than the others, look for refact­oring your tests.

Test Class Organi­zation

class SubjectTests {
  // fields
  int callCount = 0;

  // help methods
  private Subject MakeSubject() => 
    new Subject();
  // test methods
  public void Test1() {  }
  public void Test2() {  }
  public void Test3() {  }
This is just a conven­tion. Don't leave help methods and field scattered all around the test methods.

Solitary vs Sociable

Type of test that tests a unit without the involv­ement of other units.
Mocks all depend­encies of the subject under test.
Type of test that uses multiple units to verify a given behavior.
Mock only hard to manage depend­encies. (e.g. external resources)
source: Working Effect­ively with Unit Tests by Jay Fields


We have control over all it's parts.
Runs in any order.
Doesn't depend on another test.
Doesn't produce side-e­ffects.
Asserts observable behavior.
Tip: if any of these is false then it's not a unit-test.

Test - What's your name?

// 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() { ... }
The name of the test should have 3 parts:
 ­ - The behavior under test;
 ­ - The constr­aints;
 ­ - The expected behavior.

Actions on Loops

public void Test1() {
  for(var x in listOfInt) {
    Assert.That(GetValue(x), Is.True));
Tip: Multiple asserts and action taken within a loop on the same test makes us ignore some cases in case of a failure.

Avoid some Expect­ations

// Thats how it's done
// If possible use specific value, is int.Ma­xValue a valid expectation?
Assert.Th­at(x, Is.Gre­ate­rTh­an(10))
// Might be null
var result = GetObj­ect();

Assert.Th­at(­res­ult.Pr­operty, Is.True))
// Might throw exception somewhere other than action
// Look for content not types
Assert.Is­Ins­tan­ceO­fTy­pe(­result, typeof­(So­meD­ata­Mod­el));
Avoid != Never do it

Don't ignore the signs!

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 Except­ions.
Reveals missing tests. Exceptions will be tested by their usage.
Big test file
Can indicate duplic­ation or the subject under test is doing to much.
Json, xml, etc
Formatted strings of any kind reveal coupling. Except tests to format­ters.
Big file : Any file greater than 500 lines
Big section : More than 10 lines.
Many parameters : More than 3.
Many Mocks : More than 3.
Large dto: More than 10 proper­ties.

Parame­terized (NUnit)

public void Test1([Values(1, 2, 3)] int value) { }

Parame­terized (xUnit)

public void Test1(int value) { }

Parame­terized (MS Tests v2)

public void Test1(int value) { }

Isolate - Shared data

static int value = 0; 

public void Test1() {
  value = 10;

public void Test2() {
Static mutable state will eventually kill one or more tests.

Isolation - Thread safe tests

static object lockObject = new object();

public void Test1() {
  lock(lockObject) {
    // thread safe code

public void Test2() {
  lock(lockObject) {
    // thread safe code
Avoid this! Dealing with thread safety in tests adds another layer of comple­xity.


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Regular Expressions Cheat Sheet

          More Cheat Sheets by AlienEngineer