有一个专门用于单元测试的单独项目有什么优点和缺点?

发布于 2024-10-02 00:54:15 字数 66 浏览 4 评论 0原文

您能否比较将所有单元测试放在一个专门用于单元测试的项目中与将单元测试驻留在相关程序集中的优缺点?

谢谢

Can you compare the advantages and disadvantages of having all your unit tests in one project devoted to unit-tests only against having your unit-tests residing the relevant assemblies?

Thanks

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

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

发布评论

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

评论(5

尘世孤行 2024-10-09 00:54:16

对于我自己来说,有两个直接的优点。

首先,当我发送这些位时,我仅发送用于相关应用程序的代码。

另一个好处是在持续集成 (CI) 环境中,我可以将单元测试框架指向与预定义名称模式匹配的程序集列表,例如以“Test”结尾的任何程序集。

For myself, there are 2 immediate pros.

The first is that, when I ship the bits, I'm shipping only code that is used for the application in question.

The other benefit is in a continuous integration (CI) environment, I can point my unit testing framework to a list of assemblies that match a pre-defined name pattern, such as any assemblies that end in "Test".

吾家有女初长成 2024-10-09 00:54:16

主要优点是关注点分离。它们的类和测试是完全正交的,应该分开。其他优点和缺点确实可以从中衍生出来,例如用户需要应用程序,但您必须交付应用程序+测试,因为它们会被尖峰化。

The main pro is Separation of concerns. Classes and tests for them are completely orthogonal and should be separated. Other pros and cons can really be derived from it, for example user needs application, but you will have to ship application + tests, because they are spiked.

ˉ厌 2024-10-09 00:54:16

将单元测试直接包含在正在测试的代码旁边时的主要缺点是需要额外的机制来从交付的二进制文件中排除测试代码。

在单独的项目中隔离单元测试时,我能想到的唯一问题是,根据您使用的语言,测试纯内部类可能会变得困难。对于当前的 C++ 项目,我使用具有特定“单元测试”构建配置的单独项目,其中我的产品构建为静态库而不是 dll,从而允许测试项目访问任何类型或函数,无论其类型或函数如何出口规格。

The major drawback when including unit tests directly next to the code they're testing is that an additional mechanism becomes required to exclude the test code from the delivered binaries.

The only problem I can think of when isolating unit tests in a separated project is that, depending on the language you're using, it may become difficult to test purely internal classes. For a current C++ project, I'm using a separated project with a specific 'Unit tests' build configuration in which my product builds as a static library instead of a dll, thus allowing the test project to access any type or function regardless of its export specifications.

夜光 2024-10-09 00:54:16

特定于 C#:

请注意,类及其测试是紧密耦合的。分成两个装配体可以跨装配体边界延伸耦合链接。

我更喜欢将单元测试放在与他们正在测试的类相同的程序集中。对于命名空间 N 中的每个类 C,我会将单元测试放在命名空间 N.Tests 的类 CTests 中。

测试非公共类很简单。 (TDD 中常见的说法是,您不应该对私有成员进行单元测试;我同意。这里我谈论的是 internal 类和 internal 成员。)

我不知道不必处理那么多的程序集。

我应该补充一点,我不介意将我的单元测试发送给我的用户。如果它们通过拨号下载或在存储空间最小的系统上运行,情况会有所不同。

旁白:

如果可以的话,我会将单元测试放在嵌套类中,如下所示:

public class C
{
    // note 'private' accessibility. 
    [TestFixture]
    class Tests
    {
        // tests here
    }

    // Implementation of C here
}

一些优点:

  • 重命名 C 不需要重命名 CTests

  • 我确切地知道在哪里可以找到我的测试。

  • 测试和被测代码总是在一起。

这仅适用于班级规模较小的情况。一旦它们变大,文件就会变得巨大。可以使用 partial 类将它们放在单独的文件中。

不幸的是,NUnit 和 Visual Studio 的单元测试框架看不到非公共测试装置。我不认为排除非公开测试有什么好处,但这就是它们的工作原理。

Specific to C#:

Note that a class and its tests are tightly coupled. Separating in to two assemblies stretches that coupling link across assembly boundaries.

I prefer to put my unit tests in the same assembly as the class they're testing. For each class C in namespace N I will put unit tests in class CTests in namespace N.Tests.

It's trivial to tests non-public classes. (A common refrain in TDD is that you shouldn't be unit testing private members; I agree. Here I'm talking about internal classes and internal members.)

I don't have to juggle as many assemblies.

I should add that I don't mind shipping my unit tests to my users. It would be different if they were downloading over dialup, or running on systems with minimal storage.

Aside:

If I could, I would put my unit tests in a nested class, like this:

public class C
{
    // note 'private' accessibility. 
    [TestFixture]
    class Tests
    {
        // tests here
    }

    // Implementation of C here
}

Some advantages:

  • Renaming C doesn't require renaming CTests.

  • I know exactly where to find my tests.

  • A test and the code under test are always together.

This only works if your classes are small. Once they get big, the file becomes enormous. It's possible to use partial classes to put them in separate files.

Unfortunately, NUnit and Visual Studio's unit testing framework don't see non-public test fixtures. I don't believe there's an advantage to excluding non-public tests, but that's how they work.

无法言说的痛 2024-10-09 00:54:15

将它们保留在单独的程序集中的优点:

  • 不会不必要地增加正在测试的模块的复杂性,并且不会违反关注点分离。

  • 您不需要在整个解决方案中引用您的测试框架。

  • 它将使您能够(或至少使您更容易)在 CI 环境中设置您的项目。

Advantages to keeping them in seperate assemblies:

  • doesn't unnecessarily increase the complexity of the module being tested and doesn't violate separation of concerns.

  • you won't need to have your testing framework referenced throughout your solution.

  • it will enable (or at least make it easier for you) you to set up your project in a CI environment.

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