I'm practicing test driven development while implementing a network simulator: devices communicate with each other and implement little bits of functionality that respond to messages. Each device has its own view of the network topology and can be individually powered on or off.
Timing is important. If the Device
is in the simulation, has powered, and is powered off, it should be powered on after exactly two update calls.
Below is an extract of a test case I have written, trying to decompose the problem. Be warned that to a large degree I'm making the design up as I fly, so it could be the case that I haven't decomposed the objects involved enough.
let device = Device()
device.hasPower = true
device.isPoweredOn = false
// There is hidden state that we do not know about.
// Does this make it a bad test?
// Is this instead some kind of integration test?
device.update()
XCTAssertEqual(device.log.last, .poweringOn)
device.update()
XCTAssertEqual(device.log.last, .poweredOn)
XCTAssertEqual(device.isPoweredOn, true)
Notice how device
depends upon its previous state between the two assert statements.
Taking a step back, what is actually going on is I'm verifying that the state of device
is equal to a sequence of state changes:
.poweringOn
, then.poweredOn
.
The question is: how can I write effective tests when the number of elements in the sequence becomes large? (or, equivalently, when the simulation depends upon time, or equivalently, when you have an update loop).
Research
Why not look at what other people do for testing simulation software. OMNeT++ I'm looking at you.. I'm looking for an approach that doesn't involve setting up external tests. That is, I'd like to tests specific bits of functionality in my codebase because it is convenient with today's tooling and I believe it is more rigourous.
I looked at other questions on this site but couldn't find anything related to testing simulations. I searched for "depends on previous state" and "simulation testing" but couldn't find anything that matched what I was looking for. I also tried googling "multiple assets unit testing", and "simulation testing", but again the results are not relevant to my problem.