为什么要使用 testFixture 而不是 TestClass?
可以通过三种方式组织单元测试:按夹具、类或功能进行测试。但 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我尊重 Mike Two 的回应,但我断言 NUnit 团队的做法是非常错误的,并且使用
[TestFixture]
是 NUnit 表面上的语义疣。 测试类不是固定装置。从我对 JUnit 的深入研究来看,我没有找到任何将测试类作为测试装置的引用,也没有找到太多关于引用测试类的“测试装置”的讨论。相反,所有关于固定装置的 JUnit/xUnit 讨论都与设置和拆卸有关,当然,这是用于设置实际测试固定装置的常用方法。请注意,在 NUnit 2.5 中,您可以删除 [TestFixture] 注释。
更新:(2012 年 7 月)
我刚刚读了 Cucumber Book,在第 99 页上,作者 Matt Wynne 解释了使用“fixture”的起源。我引用:
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:
主要的历史原因是 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]
.既然你问了,我就查了一下。
测试夹具是运行测试之前必须建立的固定基线状态,以便结果是可预测和可重复的。在单元测试框架中,我们使用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).