我已经听过这个术语很多次了(在编程环境中),但找不到它的任何解释。有什么好的文章或解释吗?


当前回答

我想你指的是测试装置

测试夹具的目的是为了保证有一个众所周知的夹具 和固定的环境,在其中运行测试,以便结果 可重复的。有些人称之为测试上下文。 装置的例子: 用特定的、已知的数据集加载数据库 擦除硬盘并安装一个已知的干净操作系统安装 复制一组特定的已知文件 准备输入数据和设置/创建假或模拟对象

(来源:维基百科,见上面的链接)

这里还有一些来自'谷歌Test'框架文档的实际示例。

其他回答

我认为php单元测试很好地解释了这一点:

编写测试中最耗时的部分之一是编写 代码将世界设置为已知状态,然后将其返回到其 测试完成时的原始状态。这个已知的状态被称为 测试的夹具。

另外Yii文件中还描述了夹具测试的良好形状:

自动化测试需要多次执行。确保测试 过程是可重复的,我们要在一些已知的情况下运行测试 状态称为fixture。例如,测试post创建功能 在博客应用程序中,每次运行测试时,表 存储关于帖子的相关数据(例如,帖子表,评论 表)应该恢复到某个固定状态。

下面是一个简单的fixture测试示例:

<?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));
    }
}
?>

这个PHP单元测试具有名为setUp和tearDown的函数,这样在运行测试之前就可以设置数据,一旦完成,就可以将它们恢复到初始状态。

正是针对这个主题,JUnit有一个解释得很好的文档。这是链接!

文章的相关部分是:

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");
    }
}

我写这个答案是为了让自己快速了解什么是“固定装置”。

same-data-multiple-tests

测试fixture:对多个测试使用相同的数据配置 如果您发现自己正在编写两个或多个操作类似数据的测试,则可以使用测试fixture。这允许您为几个不同的测试重用相同的对象配置。

你可以在googletest上阅读更多

fixture可以用于集成测试或开发期间(假设UI开发中数据来自开发数据库)

用于数据库或测试的假用户

myproject /夹具/ 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"
    }
  }
]

你可以从django文档中阅读更多

Xamarin的。UITest解释如下:

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.

以上节选的原文链接

在Xamarin内部。UITest代码如下所示:

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.");
        }
    }
}

希望这可能对那些正在寻找更好地理解编程中的fixture的人有所帮助。

术语fixture因上下文、编程语言或框架而异。

1. 运行测试所依据的已知状态

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. 包含示例数据的文件

fixture是示例数据的一个花哨的词。固定装置可以让你 在测试之前,用预定义的数据填充测试数据库 运行。fixture是数据库独立的,并且是用YAML编写的。有 每个模型一个文件。 RubyOnRails.org

3.设置所需状态的进程。

软件测试夹具为测试过程建立系统 为它提供初始化所需的所有代码 满足任何先决条件。一个例子是 从客户站点加载具有已知参数的数据库 在运行测试之前。 维基百科