Test oracles & rollback techniques

A test is only useful if its verdict can be trusted. Two techniques make the difference, especially for automated tests: making sure each test runs on a known, clean environment (pre-conditioning, post-conditioning and rollback), and deciding reliably whether the result is right (predictive testing or a test oracle).

Anatomy of a reliable test

Prepare, operate, clean up

A test that depends on what the previous test left behind (a file, a record in a database, an open session, a modified setting) will sooner or later fail for the wrong reason, or pass when it should fail. This is one of the main causes of "flaky" tests. To avoid it, a test is organized in three parts, which can be combined depending on what you are testing:

  • Pre-conditioning ensures the initial state: the system under test is put in a known state before the test starts
  • Operating runs the operations of the test itself and produces its result; each operation that changes the system is recorded so that it can be reversed: this is the rollback
  • Post-conditioning ensures the final state: the system is left clean for the next test, whatever the result
Pre-conditioning, operating with rollback, and post-conditioning of a test on the system under test

Pre-conditioning, operating (with its rollback) and post-conditioning

Pre- and post-conditioning can be done in two ways:

  • Automatic: an operation that always brings the system to a clean state, whatever its current state (e.g. rebooting a platform, resetting a database from a dump, re-initializing a PIN)
  • Driven: the system under test (or another source, such as a property file updated during the tests) is first queried to know its current state, then only the necessary tasks are run to bring it to the expected state

Rollback

Undo exactly what the test did

Once the test is completed, the operations it performed can be reversed, in the appropriate order (usually the reverse order of execution). Only the operations that change the system need a rollback: an operation that just reads a value does not. Deciding which operations can and must be rolled back is the job of the test developer, as it depends on the system under test.

Basic tasks

The rollback only needs the identity of the task: any identifier that uniquely represents it (e.g. "delete the user created by this test").

Complex tasks

The rollback also needs properties returned when the task was performed. For instance, if the test starts a program:

pid = runProgram("/bin/prog/prog1.exe")

the rollback must remember the process identifier to be able to stop it:

killProgram(pid)

Rollback is more precise than post-conditioning, as it only undoes what the test did, and often faster than resetting the whole system. Both are often combined: rollback for the normal case, post-conditioning as a safety net when the test crashed.

Test and test case levels

Where to apply these techniques

Test management tools usually organize testing in two levels: tests, each containing one or several test cases. Pre-conditioning, operating with rollback, and post-conditioning can be applied at either level: at the test level for the expensive setup shared by all its test cases (e.g. deploying the application), at the test case level for what each test case changes (e.g. the data it creates).

Pre-conditioning, operating, rollback and post-conditioning applied to each test and test case

The same mechanism applies to tests and to test cases

Deciding whether a test passed: predictive testing vs oracle

Where does the expected result come from?

To give a verdict, a test compares the actual result with an expected result. The mechanism that provides that expected result is called a test oracle, in the broad sense. Two methodologies are commonly distinguished.

Predictive testing

The expected behavior is known in advance and written in the test: a calculated value, a message that must be displayed, a state the system must reach. This is the most common case, and it works as long as the expected results can be determined precisely when the test is written.

Predictive testing: the test compares the result of its operations with expected values it already knows

Predictive testing: the expected results are part of the test

Oracle testing

When the expected results are too numerous or too complex to be written by hand, the same operations are run both on the system under test and on a reference system trusted to behave correctly, and the results are compared. The reference can be a previous version of the product (back-to-back or regression testing of a new version), a reference implementation, possibly simplified or slower, written only for testing, or a competing implementation of the same standard. The reference goes through the same pre-conditioning, rollback and post-conditioning as the system under test.

Oracle testing: the same operations run on a reference system (the oracle) and on the system under test, and their results are compared

Oracle testing: the same operations, run on the oracle and on the system under test

Other kinds of oracles are also used when no reference system exists:

  • Specification oracles: rules derived from the specifications (e.g. "the total is always the sum of the lines")
  • Consistency and metamorphic oracles: relations that must hold between several runs (e.g. sorting an already sorted list must not change it; searching with more criteria cannot return more results)
  • Heuristic oracles: approximate checks (plausible ranges, no crash, no error in the logs), useful in exploratory and robustness testing
  • Human oracles: a tester judges the result, as in manual and exploratory testing

Finding a reliable oracle is one of the hardest problems in testing, known as "the oracle problem": a test without a trustworthy expected result can only detect crashes.

In practice

Every automation framework supports these techniques
  • xUnit frameworks (JUnit, TestNG, NUnit...): @BeforeEach / @AfterEach and @BeforeAll / @AfterAll for the test case and test levels
  • pytest: fixtures, whose code after yield performs the cleanup even when the test fails
  • Robot Framework: Suite Setup / Suite Teardown and Test Setup / Test Teardown
  • Cucumber: Before and After hooks, and Background steps in Gherkin
  • Databases: running each test inside a transaction that is rolled back at the end
  • Environments: containers or virtual machine snapshots restored before each test or suite

See also the test automation frameworks to know, baselines in testing and the QA interview questions.