为什么要使用 testFixture 而不是 TestClass?

发布于 2024-09-08 19:49:17 字数 84 浏览 2 评论 0原文

可以通过三种方式组织单元测试:按夹具、类或功能进行测试。但 TestClass 的 NUnit 属性称为 TestFixture。这其中有什么历史原因吗?

There are three ways to organize unit tests: Test per Fixture, Class or Feature. But NUnit attribute for TestClass is called TestFixture. Are there any historical reasons for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘寄 2024-09-15 19:49:17

我尊重 Mike Two 的回应,但我断言 NUnit 团队的做法是非常错误的,并且使用 [TestFixture] 是 NUnit 表面上的语义疣。 测试类不是固定装置。从我对 JUnit 的深入研究来看,我没有找到任何将测试类作为测试装置的引用,也没有找到太多关于引用测试类的“测试装置”的讨论。相反,所有关于固定装置的 JUnit/xUnit 讨论都与设置和拆卸有关,当然,这是用于设置实际测试固定装置的常用方法。

请注意,在 NUnit 2.5 中,您可以删除 [TestFixture] 注释。

更新:(2012 年 7 月)

我刚刚读了 Cucumber Book,在第 99 页上,作者 Matt Wynne 解释了使用“fixture”的起源。我引用:

将测试系统和被测系统之间的链接称为夹具有一个悠久的传统(来自测试夹具起源的硬件世界)。这就是我们在本书中称为自动化代码的“粘合代码”角色。 FIT 测试框架使用该术语的这个含义。
一些单元测试工具(例如 NUnit)通过将测试用例类本身作为固定装置来进一步混淆这个问题。对于一种无处不在的语言来说就这么多了! (Wynne 和 Hellesoy,2012)

I respect Mike Two's response, but I would assert that the NUnit team got this very wrong, and the use of [TestFixture] is a semantic wart on the face of NUnit. A test class is not a fixture. From what I've dug into with regard to JUnit, I have not found any reference to a test class as a test fixture, nor have I found much discussion about "test fixtures" referring to test classes. Rather, all the JUnit/xUnit discussion about fixtures pertain to setup and teardown, which, of course, are the common methods used to set up actual test fixtures.

Note that in NUnit 2.5, you can remove the [TestFixture] annotation.

Update: (July 2012)

I was just reading the Cucumber Book and on page 99, author Matt Wynne explains the origin of using "fixture." I quote:

There is a long tradition (coming from the hardware world, where test fixtures originated) of calling the link between the test system and the system under test a fixture. This is the "glue code" role that we've referred to in this book as automation code. The FIT testing framework uses this meaning of the term.
Some unit testing tools (such as NUnit) have further confused the issue by referring to the test case class itself as a fixture. So much for a ubiquitous language! (Wynne & Hellesoy, 2012)

还不是爱你 2024-09-15 19:49:17

主要的历史原因是 NUnit 最初是从 JUnit 直接移植的,junit 将其称为测试夹具。

NUnit 1.0 出现在我之前,但有人告诉我,它首先将 JUnit 中的所有 .java 文件重命名为 .cs 文件并尝试编译。从那里修复了它并添加了 UI。当我加入 NUnit 2.0 时,NUnit 1.0 中仍然有一个名为 IsVisualAgeForJava 的方法,因为 JUnit 当时对此有特殊的行为。

在 NUnit 2.0 中,我们的目标是使 NUnit 更加 .NETish。所以我们添加了属性和一堆其他东西。我们所有人都有 Java 背景,并且已经使用 JUnit 多年。使用[TestFixture]似乎很自然。

The main historical reason is that NUnit started life as a straight port from JUnit and junit called it test fixture.

NUnit 1.0 was before my time but I've been told it started out by renaming all of the .java files in JUnit to .cs files and trying to compile. It was fixed up from there and a UI was added. When I joined on for NUnit 2.0 there was still a method in NUnit 1.0 called IsVisualAgeForJava since JUnit had special behavior for that at the time.

In NUnit 2.0 our aim was to make NUnit more .NETish. So we added the attributes and a bunch of other stuff. All of us came from java backgrounds and had worked with JUnit for years. It seemed quite natural to use [TestFixture].

若无相欠,怎会相见 2024-09-15 19:49:17

既然你问了,我就查了一下。
测试夹具是运行测试之前必须建立的固定基线状态,以便结果是可预测和可重复的。在单元测试框架中,我们使用SetUp 和TearDown 属性/方法来创建/销毁测试装置(例如,使用正确的对象初始化实例变量)。

Now that you ask about it, I just looked it up.
A test fixture is the fixed baseline state that must be established before the tests are run, such that the results are predictable and repeatable. In unit testing frameworks, we use the SetUp and TearDown attributes/methods to create/destroy the test fixture (e.g. initialize instance variables with the right objects).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文