简体   繁体   English

测试用例在dotnet核心项目中显示不正确

[英]Test cases shown incorrectly in dotnet core project

I'm trying to create testcases for my tests in my dotnet core project. 我正在尝试为我的dotnet核心项目中的测试创建测试用例。 But resharper shows them not grouped in resharper's "unit test sessions" window 但是resharper显示它们没有在resharper的“单元测试会话”窗口中分组

My code: 我的代码:

public class Calculator
{
    public static int Multiply(int x, int y)
    {
        return x * y;
    }
}

[TestFixture]
public class CalculatorTests
{
    private static IEnumerable<TestCaseData> CalculatorTestCaseData
    {
        get
        {
            yield return new TestCaseData(3, 4, 12).SetName("Multiply 3 and 4 should be 12");
            yield return new TestCaseData(4, 5, 20).SetName("Multiply 4 and 5 should be 20");
        }
    }

    private static IEnumerable<TestCaseData> CalculatorTestCaseDataWithZero
    {
        get
        {
            yield return new TestCaseData(0, 4, 12).SetName("Multiply 0 and 4 should be 0");
            yield return new TestCaseData(5, 0, 20).SetName("Multiply 5 and 0 should be 0");
        }
    }

    [Test]
    [TestCaseSource(typeof(CalculatorTests), nameof(CalculatorTestCaseData))]
    [TestCaseSource(typeof(CalculatorTests), nameof(CalculatorTestCaseDataWithZero))]
    public void Calculate_Success(int x, int y, int expected)
    {
        Calculator.Multiply(x, y).Should().Be(expected);
    }
}

in dotnet core project resharper shows 在dotnet核心项目转播节目中

在此处输入图片说明

in dotnet framework shows 在dotnet框架中显示

在此处输入图片说明

in framework tests a grouped. 在框架测试中分组。 I need that in my dotnet core app. 我在dotnet核心应用中需要它。

Can anyone help me? 谁能帮我?

I'm using: 我正在使用:

  • dotnet core - 2.1 dotnet核心-2.1
  • Nunit - 3.10.1 Nunit-3.10.1
  • NUnitTestAdapter - 3.10.0 NUnitTestAdapter-3.10.0
  • Microsoft.NET.Test.Sdk - 15.8.0 Microsoft.NET.Test.Sdk-15.8.0
  • Resharper - 2018.2.3 Resharper-2018.2.3
  • VS community 2017 - 15.8.2 VS Community 2017-15.8.2
  • FluentAssertions - 5.4.2 FluentAssertions-5.4.2

Update 更新资料

Case when resharper doesn't recognize my tests 转售商无法识别我的测试的情况

public class Calculator
{
    public static int Multiply(MyInt x, MyInt y)
    {
        return x.Value * y.Value;
    }
}

[TestFixture]
public class CalculatorTests
{
    private static IEnumerable<TestCaseData> CalculatorTestCaseData
    {
        get
        {
            yield return new TestCaseData(new MyInt(3), new MyInt(4), new MyInt(12)).SetName("2132");
            yield return new TestCaseData(new MyInt(4), new MyInt(5), new MyInt(20)).SetName("123123asdas");
        }
    }

    [Test]
    [TestCaseSource(typeof(CalculatorTests), nameof(CalculatorTestCaseData))]
    public void Calculate_Success(MyInt x, MyInt y, MyInt expected)
    {
        Calculator.Multiply(x, y).Should().Be(expected.Value);
    }
}

public class MyInt
{
    public int Value;

    public MyInt(int value)
    {
        Value = value;
    }
}

I don't know why, but using the method .SetName() of TestCaseData seems to mess up a bit the tests, when using Resharper. 我不知道为什么,但是当使用Resharper时,使用TestCaseData .SetName()方法似乎会使测试有些混乱。 Tests were sometimes showing as Inconclusive . 测试有时显示为不确定

In order to group the unit tests correctly (ie showing not only the test class but also the test method) I decided to do some refactoring as follows: 为了正确地对单元测试进行分组(即,不仅显示测试类,还显示测试方法),我决定进行如下重构:

1) Remove the method .SetName() 1)删除方法.SetName()

public static IEnumerable<TestCaseData> CalculatorTestCaseData
{
    get
    {
        yield return new TestCaseData(3, 4, 12);
        yield return new TestCaseData(4, 5, 20);
    }
}

public static IEnumerable<TestCaseData> CalculatorTestCaseDataWithZero
{
    get
    {
        yield return new TestCaseData(0, 4, 12);
        yield return new TestCaseData(5, 0, 20);
    }
}

2) Refactor the test method 2)重构测试方法

I refactored the test method, using the Arrange-Act-Assert pattern. 我使用Arrange-Act-Assert模式重构了测试方法。 Test message is now set using the Fluent Assertions: 现在使用Fluent断言设置测试消息:

[Test]
[TestCaseSource(typeof(CalculatorTests), nameof(CalculatorTestCaseData))]
[TestCaseSource(typeof(CalculatorTests), nameof(CalculatorTestCaseDataWithZero))]
public void Calculate_Success(int x, int y, int expected)
{
    // arrange/act 
    int result = Calculator.Multiply(x, y);

    // assert
    result.Should().Be(expected, because: $"Multiply {x} and {y} should be {expected}");
}

Test Explorer - unit tests results: 测试资源管理器-单元测试结果:

Test Explorer单元测试结果

Resharper - Unit Test Sessions: Resharper-单元测试会议:

Resharper-单元测试会议

Do you have suggestions about difference between display in dotnet Core and dotnet framework? 您是否对dotnet Core和dotnet框架中的显示有区别的建议? And i need to name my test cases. 我需要命名测试用例。 Default naming is based on ToString, i must override it for all used in test cases classes 默认命名基于ToString,我必须为测试用例类中使用的所有命名覆盖它

Instead of using the method .SetName() of TestCaseData you can create a test case object and overwrite the .ToString() method as follows: 代替使用TestCaseData .SetName()方法,可以创建一个测试用例对象并覆盖.ToString()方法,如下所示:

public class CalculatorTestData
{
    public CalculatorTestData(int x, int y, int expected)
    {
        X = x;
        Y = y;
        Expected = expected;
    }

    public int X { get; }
    public int Y { get; }
    public int Expected { get; }

    /// <inheritdoc />
    public override string ToString()
    {
        return $"Multiply {X} and {Y} should be {Expected}";
    }
}

Using the CalculatorTestData object: 使用CalculatorTestData对象:

public static IEnumerable<TestCaseData> CalculatorTestCaseData
{
    get
    {
        yield return new TestCaseData(new CalculatorTestData(3, 4, 12));
        yield return new TestCaseData(new CalculatorTestData(4, 5, 20));
    }
}

public static IEnumerable<TestCaseData> CalculatorTestCaseDataWithZero
{
    get
    {
        yield return new TestCaseData(new CalculatorTestData(0, 4, 12));
        yield return new TestCaseData(new CalculatorTestData(5, 0, 20));
    }
}

And finally, refactoring the test method to use the CalculatorTestData object: 最后,重构测试方法以使用CalculatorTestData对象:

[Test]
[TestCaseSource(typeof(CalculatorTests), nameof(CalculatorTestCaseData))]
[TestCaseSource(typeof(CalculatorTests), nameof(CalculatorTestCaseDataWithZero))]
public void Calculate_Success(CalculatorTestData data)
{
    // arrange/act 
    int result = Calculator.Multiply(data.X, data.Y);

    // assert
    result.Should().Be(data.Expected, because: $"{data.X} * {data.Y} should be {data.Expected}");
}

Test Explorer - unit tests results: 测试资源管理器-单元测试结果:

测试资源管理器-单元测试结果

Resharper - unit tests results: Resharper-单元测试结果:

Resharper-单元测试会议

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

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