xUnit:多重断言或软断言
xUnit : Mutiple Assertions or Soft Assert
如何在 xUnit 中找到多个断言或软断言?我发现 Nunit 有以下能力,试图在 xUnit 中找到类似的选项。
Assert.Multiple(() =>
{
Assert.AreEqual(expectedResult1, actualResult1, "Mismatch in Score1!");
Assert.AreEqual(expectedResult2, actualResult2, "Mismatch in Score2!");
Assert.AreEqual(expectedResult3, actualResult3, "Mismatch in Score3!");
});
从 xUnit 2 开始,它 does not exist。
There are plenty of things we could do, but for many reasons we choose not to. This reasons include: (a) implementation and maintenance cost; (b) competing priorities; (c) philosophical objections to features.
In this case, you're running afoul of (c). We do not believe that a unit testing framework should ever run more than one failing assertion.
xUnit 通常比 NUnit 固执己见,出于意识形态原因,不会包含 NUnit 支持的内容。
它looks like xUnit 3 will support this,使用Assert.Multiple
。
如果您喜欢 BDD / 流畅风格的断言,您可以考虑使用这些库之一,它们都与 xUnit 完全兼容:
- FluentAssertions offers soft assertions via Assertions Scopes:
using (new AssertionScope())
{
actualResult1.Should().Be(expectedResult1, "Mismatch in Score1!");
actualResult2.Should().Be(expectedResult2, "Mismatch in Score2!");
}
- Shouldly offers soft assertions via ShouldSatisfyAllConditions()处理对象时的方法:
var mrBurns = new Person { Name = null };
mrBurns.ShouldSatisfyAllConditions(
() => mrBurns.Name.ShouldNotBeNullOrEmpty(),
() => mrBurns.Name.ShouldBe("Mr.Burns"));
将您的测试用例放入 IEnumerable<T>
并使用 Assert.All<T>()
:
var expectations = new List<Tuple<object, object>>()
{
new(expectedResult1, actualResult1),
new(expectedResult2, actualResult2),
new(expectedResult3, actualResult3),
};
Assert.All(expectations, pair => Assert.Equal(pair.Item1, pair.Item2));
现在确实存在https://github.com/xunit/assert.xunit/blob/main/MultipleAsserts.cs, after the issue 1920 got addressed. It is shipped first in the assert.xunit-tag 2.4.2-pre.19 and the NuGet-package assert.xunit 2.4.2-pre.12. See also Release Notes of xUnit.net 2.4.2 Pre-Release (build 12)
题目中的例子可以写成
Assert.Multiple(
() => Assert.AreEqual(expectedResult1, actualResult1, "Mismatch in Score1!"),
() => Assert.AreEqual(expectedResult2, actualResult2, "Mismatch in Score2!"),
() => Assert.AreEqual(expectedResult3, actualResult3, "Mismatch in Score3!"));
如何在 xUnit 中找到多个断言或软断言?我发现 Nunit 有以下能力,试图在 xUnit 中找到类似的选项。
Assert.Multiple(() =>
{
Assert.AreEqual(expectedResult1, actualResult1, "Mismatch in Score1!");
Assert.AreEqual(expectedResult2, actualResult2, "Mismatch in Score2!");
Assert.AreEqual(expectedResult3, actualResult3, "Mismatch in Score3!");
});
从 xUnit 2 开始,它 does not exist。
There are plenty of things we could do, but for many reasons we choose not to. This reasons include: (a) implementation and maintenance cost; (b) competing priorities; (c) philosophical objections to features.
In this case, you're running afoul of (c). We do not believe that a unit testing framework should ever run more than one failing assertion.
xUnit 通常比 NUnit 固执己见,出于意识形态原因,不会包含 NUnit 支持的内容。
它looks like xUnit 3 will support this,使用Assert.Multiple
。
如果您喜欢 BDD / 流畅风格的断言,您可以考虑使用这些库之一,它们都与 xUnit 完全兼容:
- FluentAssertions offers soft assertions via Assertions Scopes:
using (new AssertionScope())
{
actualResult1.Should().Be(expectedResult1, "Mismatch in Score1!");
actualResult2.Should().Be(expectedResult2, "Mismatch in Score2!");
}
- Shouldly offers soft assertions via ShouldSatisfyAllConditions()处理对象时的方法:
var mrBurns = new Person { Name = null };
mrBurns.ShouldSatisfyAllConditions(
() => mrBurns.Name.ShouldNotBeNullOrEmpty(),
() => mrBurns.Name.ShouldBe("Mr.Burns"));
将您的测试用例放入 IEnumerable<T>
并使用 Assert.All<T>()
:
var expectations = new List<Tuple<object, object>>()
{
new(expectedResult1, actualResult1),
new(expectedResult2, actualResult2),
new(expectedResult3, actualResult3),
};
Assert.All(expectations, pair => Assert.Equal(pair.Item1, pair.Item2));
现在确实存在https://github.com/xunit/assert.xunit/blob/main/MultipleAsserts.cs, after the issue 1920 got addressed. It is shipped first in the assert.xunit-tag 2.4.2-pre.19 and the NuGet-package assert.xunit 2.4.2-pre.12. See also Release Notes of xUnit.net 2.4.2 Pre-Release (build 12)
题目中的例子可以写成
Assert.Multiple(
() => Assert.AreEqual(expectedResult1, actualResult1, "Mismatch in Score1!"),
() => Assert.AreEqual(expectedResult2, actualResult2, "Mismatch in Score2!"),
() => Assert.AreEqual(expectedResult3, actualResult3, "Mismatch in Score3!"));