A test fixture is an environment used to consistently test some item, device, or piece of software. Test fixtures can be found when testing electronics, software and physical devices.
A software test fixture sets up a system for the software testing process by initializing it, thereby satisfying any preconditions the system may have. For example, the Ruby on Railsweb framework uses YAML to initialize a database with known parameters before running a test. This allows for tests to be repeatable, which is one of the key features of an effective test framework.
Setup
Test fixtures can be set up three different ways: in-line, delegate, and implicit.
In-line setup creates the test fixture in the same method as the rest of the test. While in-line setup is the simplest test fixture to create, it leads to duplication when multiple tests require the same initial data.
Delegate setup places the test fixture in a separate standalone helper method that is accessed by multiple test methods.
Implicit setup places the test fixture in a setup method which is used to set up multiple test methods. This differs from delegate setup in that the overall setup of multiple tests is in a single setup method where the test fixture gets created rather than each test method having its own setup procedures and linking to an external test fixture.
Advantages and disadvantages
The advantage of a test fixture is that it allows for tests to be repeatable since each test is always starting with the same setup. Test fixtures also ease test code design by allowing the developer to separate methods into different functions and reuse each function for other tests. Further, test fixtures preconfigure tests into a known initial state instead of working with whatever was left from a previous test run. A disadvantage is that it could lead to duplication of test fixtures if using in-line setup.
Practices to avoid
It is considered bad practice when implicit test fixtures are too general, or when a test method sets up a test fixture and does not use it during the test. A more subtle issue is if the test methods ignore certain fields within the test fixture. Another bad practice is a test setup that contains more steps than needed for the test; this is a problem seen in in-line setup. A test case is considered "unsafe" when it modifies its fixture. An unsafe test case can render subsequent tests useless by leaving the fixture in an unexpected state. It also causes the order of tests to be important: a modified fixture must be reset if more tests are to be run after an unsafe test.
Examples
Examples of fixtures include loading a database with a specific known set of data, erasing a hard disk and installing a known clean operating system installation, copying a specific known set of files, or the preparation of input data as well as set-up and creation of mock objects. Software which is used to run reproducible tests systematically on a piece of software under test is known as a test harness; part of its job is to set up suitable test fixtures. In generic xUnit, a test fixture is all the things that must be in place in order to run a test and expect a particular outcome. Frequently fixtures are created by handling setUp and tearDown events of the unit testing framework. In setUp one would create the expected state for the test and in tearDown it would clean up what had been set up. Four phases of a test:
In physical testing, a fixture is a device or apparatus to hold or support the test specimen during the test. The influence of test fixtures on test results is important and is an ongoing subject of research. Many test methods detail the requirements of test fixtures in the text of the document. Some fixtures employ clamps, wedge grips and pincer grips. Further types of construction include eccentric roller fixtures, thread grips and button head grips and rope grips. Mechanical holding apparatuses provide the clamping force via arms, wedges or eccentric wheel to the jaws. Additionally there are pneumatic and hydraulic fixtures for tensile testing that allow very fast clamping procedures and very high clamping forces.