用于单元测试运行的特殊编译器标志

发布于 2024-12-11 07:11:30 字数 222 浏览 0 评论 0原文

我有一个关于测试套件中内置的视觉工作室的问题。 VS studio 运行这些测试时是否应用了一些特殊的编译器标志?

问题描述如下。 我在我的一个类上重载了 Equals 函数。 在测试运行期间,如果它能为我提供一些额外的信息,即类中的哪些成员根本不相等,那就太好了。

因此,我想仅当应用程序在测试模式下运行时才实现一些消息。

感谢您的任何回复! 安德烈亚斯

I have a question concerning visual studios built in test suite.
Is VS studio running these tests with some special compiler flags applied?

The problem description is as following.
I have overloaded the Equals function on one of my classes.
During test runs it would be nice, if it could give me some additional information, which members in the class aren't equal at all.

Therefore I'd like to implement some messages only if the application is running in test mode.

Thanks for any reply!
Andreas

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

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

发布评论

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

评论(4

听风吹 2024-12-18 07:11:30

VS 使用当前选择的构建配置来编译/构建项目。因此,解决方案可能是自己创建一个单独的构建配置,然后为该特定构建配置中的项目使用常量(例如 TEST)。然后可以使用 #if TEST 指令或使用 [Conditional("TEST")] 属性来限制输出方法的执行。您可以配置构建服务器,始终使用特定的构建配置运行测试,这样您就会看到额外的输出。然而,当从 VS 运行测试时,您需要手动在构建配置之间切换

VS compiles/builds the projects with the currently selected build configuration. So a solution might be to create a separate build configuration yourself, and then use a constant (e.g. TEST) for the projects in that particular build configuration. The output method execution can then be restricted either with the #if TEST directive or with the [Conditional("TEST")] attribute. You could configure your build server te allways run the tests with that particual build configuration, so you would see additional output. You would however need to switch between the build configurations manually when running the tests from VS

没有心的人 2024-12-18 07:11:30

创建一个新的解决方案配置“测试”(如果您还没有)并切换到它。打开项目设置,切换到“构建”选项卡并定义一个新符号 TEST。按确定。

将您的 Equals 实现更改为

public override bool Equals (object obj)
{
    #if TEST
     // Your implementation
    #else
      return base.Equals (obj);
    #endif
}

这将为您的测试配置编译不同的方法体。

Create a new solution configuration "Test" (if you don't have it yet) and switch to it. Open project settings, switch to the Build tab and define a new symbol TEST. Press OK.

Change your Equals implementation to

public override bool Equals (object obj)
{
    #if TEST
     // Your implementation
    #else
      return base.Equals (obj);
    #endif
}

This will compile a different method body for your test configuration.

镜花水月 2024-12-18 07:11:30

我不得不说我不喜欢在代码中到处放置条件编译的想法。它使阅读和调试代码变得更加困难。

也许您应该退后一步,意识到您有两组不同的算法来确定对象是否相等。您可以使用策略设计模式将此代码从 Equals 方法中分解出来。

然后在运行时,您可以通过依赖注入选择 Equals 方法的策略,也可以在基类中选择像这样的简单函数:

public override bool Equals (object obj)
{
    if ( EqualsStrategy != null)
    {
        return EqualsStrategy.Equals(this,object);
    }
    else
    {
       return base.Equals(obj);
    }
}

在单元测试中,您将使用要使用的函数来初始化 EqualsStrategy。

I have to say I don't like the idea of putting conditional compilation everywhere in your code. It makes it harder to read and debug your code.

Maybe you should take a step back and realize you have two different sets of algorithms for determining if an object is equal. You could factor this code out of the Equals method by using a Strategy Design Pattern.

Then at runtime you could select the Strategy for the Equals method trough Dependency Injection or maybe a simple function like this in your base class:

public override bool Equals (object obj)
{
    if ( EqualsStrategy != null)
    {
        return EqualsStrategy.Equals(this,object);
    }
    else
    {
       return base.Equals(obj);
    }
}

In your unit testing you would initialize EqualsStrategy with the function you want to use.

╄→承喏 2024-12-18 07:11:30

我强烈建议不要在您的应用程序中插入任何仅测试代码。 (单元)测试的目的是测试软件的生产质量,而不是执行仅测试代码。

如果您的仅测试代码可以正常工作,但生产版本已损坏,那么您的测试将毫无价值。

相反,您应该以只有一件事可能出错的方式编写测试。这样,如果测试失败,您就已经知道出了什么问题。因此,如果您正在测试考虑 2 个属性的 equals 方法,请编写一组小型测试来验证这两个属性的所有可能组合中会发生什么,并验证 Equals 方法的结果。

之后,您可以确信 Equals 方法已正确实现,并且无需在其他任何地方对其进行测试。

另一个解决方案可能是添加一个执行额外日志记录的测试助手。我喜欢为此使用扩展方法。例如:

public static class TestExtensions
{
  public static void ShouldEqual( this YourType subject, YourType other )
  {
     // Check parameters for null here if needed
     if( !subject.Equals( other ) )
     {
       // custom logging here
       Assert.Fail("Objects are not equal"); // test fails
     }
  }
}

I'd strongly recommend against inserting any test-only code in your application. The purpose of a (unit-)test is to test the production quality of the software, not to exercise test-only code.

If your test-only code is working find but the production version is broken your test would be worthless.

Instead, you should write your test in such a way that only one single thing can be wrong. That way if the test fails, you already know what's wrong. So, if you are testing an equals method that considers 2 properties, write a set of small tests that verifies what happens in all possible combinations of the two properties and verify the result of the Equals method.

After that you can be confident that the Equals method is correctly implemented and you don't have to test it anywhere else.

Another solution could be to add a test helper that performs additional logging. I like to use extension methods for this. For example:

public static class TestExtensions
{
  public static void ShouldEqual( this YourType subject, YourType other )
  {
     // Check parameters for null here if needed
     if( !subject.Equals( other ) )
     {
       // custom logging here
       Assert.Fail("Objects are not equal"); // test fails
     }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文