Creating a Test Application

Igloo test applications are created as command line executables that either succeeds with a return value of zero, or fails with a return value corresponding to the number of failing tests.

The main method of the application should look as follows:

#include <igloo/igloo.h> using namespace igloo; int main() { return TestRunner::RunAllTests(); }

This will automatically run all registered tests in the application.

Contexts

Igloo considers a test application to be an executable specification of the application that's under test. The test application contains one or more contexts that each describe a state of the application under test and a set of specifications for what should hold true in that context.

#include <igloo/igloo.h> using namespace igloo; Context(ANewlyStartedGame) { Spec(ShouldHaveAnEmptyBoard) { Assert::That(game.Positions(), Has().All().EqualTo(EmptyPosition)); } Game game; };

Nested Contexts

Igloo enables you to create nested contexts. The inner context inherits and augments the properties of the outer context. This is a powerful feature that lets you organize your contexts in a way that enables you to create just the right amount of setup for each context.

#include <igloo/igloo.h> using namespace igloo; Context(ANewlyStartedGame) { Spec(ShouldHaveAnEmptyBoard) { Assert::That(game.Positions(), Has().All().EqualTo(EmptyPosition)); } Context(PlayerOneIsSelectedToStart) { void SetUp() { Parent().game.Select(PlayerOne); } Spec(ItShouldBePlayerOnesTurn) { Assert::That(Parent().game.NextPlayer(), Equals(PlayerOne)); } }; Game game; };

The Parent() operation is used to access the context directly outside the current context.

Setup and TearDown

To make sure that each Spec method starts with a known state and that different Spec methods can be executed in isolation from each other, you can override the methods SetUp and TearDown in your context.

#include <igloo/igloo.h> using namespace igloo; Context(NameOfContext) { void SetUp() { /* Setup code */ } void TearDown() { /* Tear down code */ } // ... };

The SetUp method is called just before a Spec is executed. Here you can put code that initializes all you need in your tests. The TearDown method is called each time a Spec has been executed. Here you can put code that performs the clean up necessary to ensure that the next Spec isn't affected by the outcome of the current Spec.