How to be an expert test writer.

It's harder than you think

Good unit tests are:

  • Fast (milliseconds)

  • Repeatable (run multiple times)

  • Self Contained (does not need other tests)

  • Precise (pin pointing target)

  • Resilient (unaffected by other changes)

  • Sensitive (avoids false negatives)

  • Specific (avoids false positives)

     

[Test]
public void testMeasurementSupervisor_patientAndDevice_loadsWarning(){
    Mock<Person>patient = new Mock<Person>(); // Self Contained
    Mock<Device>heartRateMonitor = new Mock<HeartRateMonitor>(); // Fast
    patient.Setup(p => p.isMale()).Returns(() => true); // Precise
    heartRateMonitor.Setup(hr => hr.heartRateOver5Minutes()).Returns(() => MAX_MALE_HEARTRATE + 10);
    bool sexDinstinction = loadSexDistinctionSettings(); // Repeatable
    MeasumentSupervisor ms = new MeasumentSupervisor<HeartRateMonitor>(sexDinstinction, MAX_MAKE_HEARTRATE); // Resilient

    AnalisisResult analisysResult = ms.analize(patient, heartRateMonitor);

    Assert.isNotNull(analisysResult);
    if (sexDinstinction) {
        Assert.isTrue(analisysResult.Critical); // Sensitive
    } else {
        Assert.isTrue(analisysResult.Warning); // Specific
    }
}

Ensuring you apply the criteria

  • Write testable code (test driven development)

  • Checklists (simple and effective)

  • Code coverage (branch coverage / statement coverage)

  • Property based testing (contracts)

  • Mutation Testing (specificity)

  • Static Analysis (tests are code)

  • Capture Statistics (beyond pass/fail)

How to be an expert test writer.

By Rodolfo Hansen

How to be an expert test writer.

  • 92