Test Fixtures
Test your functions!
CliTester is easy to use, but this page talks about the usage of this library to be able to use it in your console application.
Fixture types
CliTester provides functionality that allows you to define test fixtures that are of the following types:
Unconditional fixtures: Test fixtures that fall into this category tests against functions that don't return a value (that is, returning
void
).Conditional fixtures: Test fixtures that fall into this category tests against functions that return a value (that is, returning a value other than
void
).
Unconditional fixtures
Fixtures that don't compare the returned value with the expected value and that the function being tested returns void
are unconditional fixtures. This type can be created using the constructor of either a generic version of the FixtureUnconditional
class (i.e. you are required to provide a delegate type that returns void
, such as Action
or Action<string>
) or a non-generic one for dynamic usage.
Unconditional fixtures can be defined like these example declarations:
Taking into consideration the example target functions that are defined in the UnconditionalFunctions
static class like this:
Conditional Fixtures
In the other hand, fixtures that compare the returned value with the expected value and that the function being tested has a return value are conditional fixtures. This type can be created using the constructor of either a generic version of the FixtureConditional
class (i.e. you are required to provide a delegate type that supports return value, such as Func<int>
or Func<double, double, double>
) or a non-generic one for dynamic usage.
Unconditional fixtures can be defined like these example declarations:
Taking into consideration the example target functions that are defined in the ConditionalFunctions
static class like this:
Running the fixtures
When you need to run the fixtures, you'll need to take these tips into account:
Unconditional tests that are defined using the generic version of the
FixtureUnconditional
class can be run using theRunTest<TDelegate>()
function in theFixtureRunner
static class.Unconditional tests that are defined using the non-generic version of the
FixtureUnconditional
class can be run using theRunUnconditionalTest()
function in the fixture runner class.Conditional tests that are defined using the generic version of the
FixtureConditional
class can be run using theRunTest<TDelegate, TValue>()
function in theFixtureRunner
static class, but you are required to provide both the delegate type and the value type in theRunTest()
type parameters.Conditional tests that are defined using the non-generic version of the
FixtureConditional
class can be run using theRunConditionalTest<TValue>()
function in theFixtureRunner
static class, but you are required to provide the value type in theRunConditionalTest()
type parameter.Test fixtures can also be run with the
RunGeneralTest()
function if you don't know whether the fixture is unconditional or conditional.
All the fixture runners return a Boolean value indicating whether the test passed or failed. If the test has passed, then the exception output parameter value is null
. Otherwise, it's populated with exception information that tells you what happened.
Fixture selector
To be able to make your console demo application more interactive, you can leverage the use of the fixture selector function that allows the end user to select a test fixture from the full-screen menu. This requires making a new array of type Fixture[]
that stores all the test fixtures that you want. An example of array declaration of all the test fixtures (unconditional or not) is:
After that, you can use this variable to launch the fixture selector like this:
If you've defined all the test fixtures correctly, you should now see a menu that looks similar to this:
For each test, there is a status box that indicates whether the test has been run, has succeeded, or has failed.
[ ]
: For tests that are yet to run, this is shown.[*]
: For tests that have succeeded, this is shown.[X]
: For tests that have failed, this is shown.
You can press ENTER to start a test fixture. If the test has succeeded, you'll get a green text saying that the test has passed. If the test has failed, you'll get a red text saying that the test has failed with an error message that may tell you why.
The fixture selector requires a working terminal which meets Terminaux's requirements.
Last updated