简体   繁体   English

运行所有测试时单元测试失败,但在调试时通过

[英]Unit Tests failing when I Run All Tests but pass when I Debug

I'm using NUnit3 in Visual Studio 2017 and doing TDD.我在 Visual Studio 2017 中使用 NUnit3 并执行 TDD。 Something really strange is happening since I updated my code to make my latest test pass.自从我更新了我的代码以使我的最新测试通过后,发生了一些非常奇怪的事情。

Now, 3 of my other tests are failing when I click Run All Tests, as below:现在,当我单击“运行所有测试”时,我的其他 3 个测试失败,如下所示:

在此处输入图片说明

It is telling me that the actual and expected values in my Assert method are not equal.它告诉我 Assert 方法中的实际值和预期值不相等。

However, when I put a breakpoint at the line where the Assert method is and start debugging, the stacktrace is showing that expected and actual are the same value and then the test passes, as below:但是,当我在 Assert 方法所在的行放置断点并开始调试时,堆栈跟踪显示预期和实际值相同,然后测试通过,如下所示:

在此处输入图片说明

Am I doing something stupid or could there be a bug in VS2017 or NUnit or something?我是在做一些愚蠢的事情,还是 VS2017 或 NUnit 中可能存在错误?

This ever happen to anyone else?这曾经发生在其他人身上吗?

[Edit: I should probably add that I have written each test as a separate class] [编辑:我应该补充一点,我已经将每个测试作为一个单独的类编写]

The failing tests share a resource that affects them all when tested together.失败的测试共享一个资源,当一起测试时会影响它们。 Recheck the affected tests and their subjects.重新检查受影响的测试及其主题。

You should also look into static fields or properties in the subjects.您还应该查看主题中的静态字段或属性。 They tends to cause issues if not used properly when designing your classes.如果在设计类时使用不当,它们往往会导致问题。

Some subtle differences might occur.可能会出现一些细微的差异。 For instance if a first test change a state which affects the behavior of a second test, then the outcome of this 2nd test may not be the same if I run it alone.例如,如果第一个测试改变了影响第二个测试行为的状态,那么如果我单独运行第二个测试的结果可能会不同。

An idea to help understand a test failure when a breakpoint can't be used, could be to add logging.在无法使用断点时帮助理解测试失败的一个想法可能是添加日志记录。

Anyway, to answer your questions:无论如何,回答您的问题:

This ever happen to anyone else?这曾经发生在其他人身上吗?

Yes是的

Am I doing something stupid or could there be a bug in VS2017 or NUnit or something?我是在做一些愚蠢的事情,还是 VS2017 或 NUnit 中可能存在错误?

I bet that it's neither: just a case a bit more subtle我敢打赌这两者都不是:只是一个更微妙的案例

I experienced a similar issue in Visual Studio 2017 using MSTest as the testing framework.我在使用 MSTest 作为测试框架的 Visual Studio 2017 中遇到了类似的问题。 Assertions in unit tests were failing when the tests were run but would pass when the unit tests were debugged.单元测试中的断言在运行测试时失败,但在调试单元测试时会通过。 This was occurring for a handful of unit tests but not all of them.这发生在少数单元测试中,但不是全部。 In addition to the Assertion failures, many of the units tests were also failing due to a System.TypeLoadException ( Could not load type from assembly error ).除了断言失败之外,许多单元测试也由于 System.TypeLoadException( 无法从程序集错误加载类型)而失败。 I ultimately did the following which solved the problem:我最终做了以下解决问题的方法:

  1. Open the Local.testsettings file in the solution打开解决方案中的Local.testsettings文件
  2. Go to the "Unit Test" settings转到“单元测试”设置
  3. Uncheck the "Use the Load Context for assemblies in the test directory."取消选中“对测试目录中的程序集使用加载上下文”。 checkbox复选框

After taking these steps all unit tests started passing when run.采取这些步骤后,所有单元测试在运行时开始通过。

I encountered this phenomenon myself, but found the cause quite easily.我自己也遇到过这种现象,但很容易找到原因。 More concretely, I tested some matrix calculations, and in my test class I defined data to calculate with as a class variable and performed my calculations with it.更具体地说,我测试了一些矩阵计算,在我的测试类中,我将要计算的数据定义为类变量并用它执行计算。 My matrix routines, however, modified the original data, so when I used "run tests" on the test class, the first test corrupted the data, and the next test could not succeed.然而,我的矩阵例程修改了原始数据,所以当我在测试类上使用“运行测试”时,第一个测试破坏了数据,下一个测试无法成功。

The sample code below is an attempt to show what I mean.下面的示例代码试图说明我的意思。

[TestFixture]
public void MyTestClass()
{
    [Test]
    public void TestMethod1()
    {
        MyMatrix m = new MyMatrix();

        // Method1() modifies the data...
        m.Method1(_data);
    }

    [Test]
    public void TestMethod2()
    {
        MyMatrix m = new MyMatrix();

        // here you test with modified data and, in general, cannot expect success
        m.Method2(_data);
    }

    // the data to test with
    private double[] _data = new double[1, 2, 3, 4]{};
}

暂无
暂无

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

相关问题 单元测试在“全部运行”时失败,但在单次运行时通过 - Unit tests fail on Run All but pass when are run single 我正在尝试在Visual Studio中运行所有测试,但是当我运行测试时,第一个测试将通过,但所有其他测试将失败 - I am trying to run all tests in visual studio, but when I run the the tests the first one will pass, but all the others will fail Flurl&HttpTest:单元测试在全部运行时失败,但在单独运行时通过 - Flurl & HttpTest: Unit tests fail when Run All, but pass when run individually 我怎样才能让Resharper在调试中运行测试,当我得到调试时“不确定:测试不运行” - How can I get Resharper to run tests in debug, when I get debug is “Inconclusive: Test not run” 单元测试在一起运行时失败,单独传递 - Unit Tests fail when run together, pass individually 当我运行所有测试时,不会调用 ClassInitialize - ClassInitialize doesn't get called when I run all tests 使用“在解决方案中运行所有测试”时测试失败 - tests failing when “Run All Test in Solution” is used 单元测试在Visual Studio中“全部运行”但单独传递时失败 - Unit Tests Fail when “Run All” in Visual Studio but passes individually 为什么使用HostType(“ Moles”)进行的单元测试中的断言在单独运行时可以通过,而在一组测试中运行时却失败? - Why would an assert on a unit test with HostType(“Moles”) pass when run individually, but fail when run with a group of tests? 大容量运行时单元测试失败,并且无法写入IPC端口 - Unit tests failing when large volume are run with Failed to write to an IPC Port
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM