简体   繁体   English

在发布模式下运行NUnit测试

[英]Run NUnit tests in release mode

I have a problem. 我有个问题。

#if DEBUG
        ca.ConveyancingAttorneyID = "C0B68DC3-C396-4264-880B-1A699C53A8CE";
        ca.ConveyancingFirmID = "C0B68DC3-C396-4264-880B-1A699C53A8CE";
#endif

When we are running in debug mode, we hardcode the identifier. 在调试模式下运行时,我们对标识符进行硬编码。 In release, it's obviously dynamically set. 在发行版中,显然是动态设置的。

Now when it comes to unit testing the mapping code, I've had to do this : (initally I just tested the 'Release case', which would then alwasy fail in Debug mode) 现在,当涉及到对映射代码进行单元测试时,我必须这样做:(最初我只是测试了“发布案例”,然后在调试模式下总是失败了)

#if RELEASE
        Guid id = new Guid("1A92CE5A-D956-486B-9B65-0A3630EF0285"); 
#endif
#if DEBUG
        Guid id = new Guid("C0B68DC3-C396-4264-880B-1A699C53A8CE"); 
#endif

This is not ideal. 这是不理想的。 And having to remember (and remind teammates) to always run the Unit Tests in Release is also a hassle. 而且必须记住(并提醒队友)始终在发布中运行单元测试也很麻烦。 We don't have a continuous integration setup, so the tests are only ever run locally. 我们没有持续的集成设置,因此测试仅在本地运行。

Is there a way to force NUnit / Visual Studio Testrunner / Resharper Testrunner to always run tests in Release mode? 有没有一种方法可以强制NUnit / Visual Studio Testrunner / Resharper Testrunner始终以发布模式运行测试? Or is there a better way to approach this, keeping in mind that these kind of '#if DEBUG' codeblocks are quite abundant in the solution. 还是有更好的方法来解决此问题,请记住,解决方案中这类“ #if DEBUG”代码块非常丰富。

如果必须运行Release,请使用参数/ config:Release

I'm not sure we know enough about the structure of your project to answer this in an adequate way. 我不确定我们对您的项目的结构是否足够了解,无法以适当的方式回答这个问题。 It basically depends on where and how you are using these values. 它基本上取决于您在哪里以及如何使用这些值。

Ideally, you should fetch these IDs from somewhere via an interface. 理想情况下,您应该通过接口从某个地方获取这些ID。 That would let you to fake and switch the implementation behind that interface, allowing you to return hard-coded dummy values when needed to test the logic where the values are used. 这样一来,您就可以伪造并在该接口后面切换实现,从而可以在需要测试使用值的逻辑时返回硬编码的伪值。

You could set up a configuration option allowing you to specify whether to use the real implementation, or a dummy data provider to allow switching without having to recompile. 您可以设置配置选项,以指定是使用实际实现,还是设置虚拟数据提供程序以允许进行切换而无需重新编译。

Update: Since the above was not really a great answer to your specific question, here's another attempt: Under Resharper > Options > Tools > Unit Testing , there is an option Run tests from: , where you can specify a folder to use instead of the default project output folder. 更新:由于上述内容并不是您对特定问题的真正答案,因此,这是另一种尝试:在Resharper > Options > Tools > Unit Testing ,有一个选项Run tests from Resharper > Options > Tools > Unit Testing您可以在其中指定要使用的文件夹,而不是默认项目输出文件夹。

This may be a long shot, but perhaps you can configure VS to build a Release version of your project to some specific folder, and refer to that for testing, even if the main project build configuration is set to Debug? 这可能需要很长时间,但是即使主项目构建配置设置为Debug,也可以配置VS将项目的Release版本构建到某个特定的文件夹,并参考该版本进行测试。

Remember case sensitivity matters when using arguments eg /config:Release from Xeon's answer otherwise you'll get a misleading error: 请记住,在使用参数(例如/ config:Xeon的答案中的版本)时,区分大小写很重要,否则会产生误导性错误:

Invalid : Path_to_your_file
File type is not supported error

(not enough reputation to comment) (信誉不足,无法发表评论)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM