What are fixtures in programming?
Asked Answered
B

6

349

I have heard of this term many times (in the context of programming) but couldn't find any explanation of what it meant. Any good articles or explanations?

Brant answered 22/8, 2012 at 10:42 Comment(0)
T
346

I think you're referring to test fixtures:

The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable. Some people call this the test context.

Examples of fixtures:

  • 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
  • Preparation of input data and set-up/creation of fake or mock objects

(source: wikipedia, see link above)

Here are also some practical examples from the documentation of the 'Google Test' framework.

Triglyceride answered 4/2, 2013 at 10:6 Comment(0)
J
39

The term fixture varies based on context, programing language or framework.

1. A known state against which a test is running

One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test. PHP-Unit documentation

A test fixture (also known as a test context) is the set of preconditions or state needed to run a test. The developer should set up a known good state before the tests, and return to the original state after the tests. Wikipedia (xUnit)

2. A file containing sample data

Fixtures is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent and written in YAML. There is one file per model. RubyOnRails.org

3. A process that sets up a required state. 

A software test fixture sets up the system for the testing process by providing it with all the necessary code to initialize it, thereby satisfying whatever preconditions there may be. An example could be loading up a database with known parameters from a customer site before running your test. Wikipedia

Joselynjoseph answered 28/9, 2019 at 18:32 Comment(0)
M
33

I think PHP-unit tests have very good explaining of this:

One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test.

Also Yii documents described fixtures test in a good shape:

Automated tests need to be executed many times. To ensure the testing process is repeatable, we would like to run the tests in some known state called fixture. For example, to test the post creation feature in a blog application, each time when we run the tests, the tables storing relevant data about posts (e.g. the Post table, the Comment table) should be restored to some fixed state.

Here's a simple example of fixtures test:

<?php
use PHPUnit\Framework\TestCase;

class StackTest extends TestCase
{
    protected $stack;

    protected function setUp()
    {
        $this->stack = [];
    }

    protected function tearDown()
    {
        $this->stack = [];
    }

    public function testEmpty()
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush()
    {
        array_push($this->stack, 'foo');
        $this->assertEquals('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop()
    {
        array_push($this->stack, 'foo');
        $this->assertEquals('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}
?>

This PHP unit test has functions with names setUp and tearDown so that before running your tests you setup your data and once finished you can restore them to the initial state.

Marsala answered 31/7, 2017 at 5:32 Comment(0)
W
10

Exactly to that topic, JUnit has a well explained doc. Here is the link!

The related portion of the article is:

Tests need to run against the background of a known set of objects. This set of objects is called a test fixture. When you are writing tests you will often find that you spend more time writing the code to set up the fixture than you do in actually testing values.

To some extent, you can make writing the fixture code easier by paying careful attention to the constructors you write. However, a much bigger savings comes from sharing fixture code. Often, you will be able to use the same fixture for several different tests. Each case will send slightly different messages or parameters to the fixture and will check for different results.

When you have a common fixture, here is what you do:

Add a field for each part of the fixture Annotate a method with @org.junit.Before and initialize the variables in that method Annotate a method with @org.junit.After to release any permanent resources you allocated in setUp For example, to write several test cases that want to work with different combinations of 12 Swiss Francs, 14 Swiss Francs, and 28 US Dollars, first create a fixture:

public class MoneyTest {
    private Money f12CHF;
    private Money f14CHF;
    private Money f28USD;

    @Before public void setUp() {
    f12CHF= new Money(12, "CHF");
    f14CHF= new Money(14, "CHF");
    f28USD= new Money(28, "USD");
    }
}
Wheelwright answered 24/5, 2017 at 9:13 Comment(0)
F
3

I'm writing this answer as quick note for myself on what is "fixture".

same-data-multiple-tests

Test Fixtures: Using the Same Data Configuration for Multiple Tests If you find yourself writing two or more tests that operate on similar data, you can use a test fixture. This allows you to reuse the same configuration of objects for several different tests.

you can read more at googletest

fixtures can be used for during integration test or during development (lets say ui development where data is comming from development database

fake users for database or testing

myproject/fixtures/my_fake_user.json

[
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Lennon"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]

you can read more from django docs

Ftlb answered 28/7, 2020 at 4:23 Comment(0)
N
2

In Xamarin.UITest it is explained as following:

Typically, each Xamarin.UITest is written as a method that is referred to as a test. The class which contains the test is known as a test fixture. The test fixture contains either a single test or a logical grouping of tests and is responsible for any setup to make the test run and any cleanup that needs to be performed when the test finishes. Each test should follow the Arrange-Act-Assert pattern:

  • Arrange – The test will setup conditions and initialize things so that the test can be actioned.
  • Act – The test will interact with the application, enter text, pushing buttons, and so on.
  • Assert – The test examines the results of the actions performed in the Act step to determine correctness. For example, the application may verify that a particular error message is displayed.

Link for original article of the above Excerpt

And within Xamarin.UITest code it looks like following:

using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Queries;

namespace xamarin_stembureau_poc_tests
{
    [TestFixture(Platform.Android)]
    [TestFixture(Platform.iOS)]
    public class TestLaunchScreen
    {
        IApp app;
        Platform platform;

        public Tests(Platform platform)
        {
            this.platform = platform;
        }

        [SetUp]
        public void BeforeEachTest()
        {
            app = AppInitializer.StartApp(platform);
        }

        [Test]
        public void AppLaunches()
        {
            app.Screenshot("First screen.");
        }

        [Test]
        public void LaunchScreenAnimationWorks()
        {
            app.Screenshot("Launch screen animation works.");
        }
    }
}

Hope this might be helpful to someone who is in search of better understanding about Fixtures in Programming.

Newel answered 8/11, 2018 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.