A Philosophy of Testing 7: Code for Testing

Testing is a Critical Requirement

  • Be structured in such a way as to allow the test harness to fake and instrument the various subsystems as necessary.
  • Provide “hooks” that allow the tests to deeply understand what is going on in the running code, again as necessary.

Structuring for Tests: zio-test

  • Clock, which lets you fetch the current time and schedule events.
  • Console, which handles console I/O.
  • Random, which lets you obtain random numbers and strings.
  • System, which provides access to environment variables and properties.
  • TestClock, which lets you jump the time forward or pin it to a specific time.
  • TestConsole, which lets you specify what console input will be provided, and gives you access to the any output being sent to the system console by the program.
  • TestRandom, which lets you specify the exact random values that will be generated.
  • TestSystem, which lets you provide test values for the environment variables.

Structuring for Tests: RqClient

TestHooks

  • What happens is a side-effect. For example, a call to the service results in a change to an in-memory value.
  • Nothing happens, and that’s good. For example, this call is redundant with the current state, and we intentionally don’t want to change anything.
trait TestHooks {
def event(evt: => Any): Unit
}
class TestHooksImpl() extends TestHooks {
def event(evt: => Any): Unit = {}
}
class TestTimeTestHooks() extends TestHooks {
var eventsRaised: List[Any]
def event(evt: => Any): Unit = synchronized {
eventsRaised = evt :: eventsRaised
}
}
case class InputDiscarded(name: String)
...
if (needToDoSomethingTo(name)) {
goDoTheThingTo(name)
} else {
testHooks.event(InputDiscarded(name))
}
callThatShouldNotDoAnythingWith(name)
eventually {
assert(testHooks.eventsRaised.exists(_ == InputDiscarded(name)))
}

Summary

Lifelong programmer and software architect, specializing in online social tools and (nowadays) Scala. Architect of Querki (“leading the small data revolution”).

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What Skills to Look for When Hiring a Python Developer

Creating a minimal kernel development setup using QEMU and Archlinux

INFT Product

Flink on Zeppelin — (Part 2). Batch

Developer First Culture & Collectives at Stack Overflow

The Book Every Programmer Should Read

Start programming within 1 min: Python tutorial for complete beginners

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mark "Justin" Waks

Mark "Justin" Waks

Lifelong programmer and software architect, specializing in online social tools and (nowadays) Scala. Architect of Querki (“leading the small data revolution”).

More from Medium

A Philosophy of Testing 8: Summary

Scala: for-comprehension stops at the empty box

Testing AMQP Consumer reconnect with testcontainers

[Scala] Implicit Parameters