C# 为类似的测试创建多个测试方法

C# 为类似的测试创建多个测试方法,c#,unit-testing,mstest,C#,Unit Testing,Mstest,我有一种重置两个输入值的方法: public void ResetLocation(ref ushort x, ref ushort y) { x = 0x7fff; y = 0x7fff; } 对于这方面的单元测试,我创建了两种测试方法: [TestMethod] public void ResetLocation_GivenReferenceValues_ValueXShouldBeReset() { ushort x = -5; ushort y = 0;

我有一种重置两个输入值的方法:

public void ResetLocation(ref ushort x, ref ushort y) {
    x = 0x7fff;
    y = 0x7fff;
}
对于这方面的单元测试,我创建了两种测试方法:

[TestMethod]
public void ResetLocation_GivenReferenceValues_ValueXShouldBeReset() {
    ushort x = -5;
    ushort y = 0;
    SomeController someController = new SomeController();

    someController.ResetLocation(ref x, ref y);

    Assert.AreEqual(0x7fff, x);
}

[TestMethod]
public void ResetLocation_GivenReferenceValues_ValueYShouldBeReset() {
    ushort x = 0;
    ushort y = -3;
    SomeController someController = new SomeController();

    someController.ResetLocation(ref x, ref y);

    Assert.AreEqual(0x7fff, y);
}
到目前为止,我收集的关于单元测试的“最佳实践”的信息是,每个单元测试应该只有一个断言。但是在这种情况下,在同一个测试单元中同时测试
x
y
是否更有意义(在一个测试中测试两个断言)


在这样的单元测试中有两个断言是件坏事吗?

我不认为在单个单元测试中添加多个断言是件坏事。要注意的主要事情是确保测试仍然可以理解并且易于维护。 换句话说:不要创建复杂到需要测试本身的测试:-)

我甚至不介意在使用一系列输入测试代码有意义的情况下将我的断言放在循环中

这方面的一个变化是利用内置测试框架功能,使用不同的输入重新运行测试。我曾经和努尼特一起做过这件事
请参阅此处的更多信息:

尽管可能不支持ref参数,但几乎所有其他测试框架都支持参数化测试。因此,您将编写一个测试,但使用一系列值对测试进行属性化,这些值将传递给测试并运行一次或多次。我建议转移到一个不同的框架,比如xUnit,以获得单元测试的一些最新进展。
[TestMethod]
public void ResetLocation_GivenReferenceValues_BothValuesShouldBeReset() {
    ushort x = -5;
    ushort y = 3;
    SomeController someController = new SomeController();

    someController.ResetLocation(ref x, ref y);

    Assert.AreEqual(0x7fff, x);
    Assert.AreEqual(0x7fff, y);
}