C# 使用专用nunit测试don';不要重复我的话
我是TDD开发的新手,我刚刚开始使用Nunit 3.7.1、C#和.NET Framework 4.7进行一些测试 我有一门考试课:C# 使用专用nunit测试don';不要重复我的话,c#,unit-testing,nunit,C#,Unit Testing,Nunit,我是TDD开发的新手,我刚刚开始使用Nunit 3.7.1、C#和.NET Framework 4.7进行一些测试 我有一门考试课: [TestFixture] class ProcessTrzlExportTest { private ImportTrzlBatch _import; [SetUp] public void SetUpImportTrzlBatch() { _import = new ImportTrzlBatch();
[TestFixture]
class ProcessTrzlExportTest
{
private ImportTrzlBatch _import;
[SetUp]
public void SetUpImportTrzlBatch()
{
_import = new ImportTrzlBatch();
}
[Test]
public void ShouldThrowArgumentExceptionWithNullPath()
{
// Arrange
string path = null;
// Act
ActualValueDelegate<object> testDelegate = () => _import.LoadBatchFile(path);
// Assert
Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void ShouldThrowArgumentExceptionWithEmptyPath()
{
string path = string.Empty;
// Act
ActualValueDelegate<object> testDelegate = () => _import.LoadBatchFile(path);
// Assert
Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>());
}
[Test]
public void ShouldThrowArgumentExceptionWithWhiteSpacesPath()
{
string path = " ";
// Act
ActualValueDelegate<object> testDelegate = () => _import.LoadBatchFile(path);
// Assert
Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>());
}
}
我正在测试路径
是否不为null、空或空白。我有三种测试方法,用三种不同的输入测试同一种方法
我可以使用私有测试方法不重复代码,并使用三种不同的路径从这三种方法调用它吗?另一个问题是我正在使用这个:
ActualValueDelegate testDelegate=()=>\u import.LoadBatchFile(路径)代码>
测试它是否抛出ArgumentNullException
是否有其他方法可以在不使用委托的情况下测试是否引发异常?
顺便说一句,我复制了代码来检查它是否抛出ArgumentNullException
,因此回答:对于这些场景,我倾向于使用TestCase(见下文)。这样,您只需编写一次测试,并且只需指定要测试的差异值。在您的情况下,为null和空字符串
[TestCase(null)]
[TestCase("")]
public void ShouldThrowArgumentException(string path)
{
// Act
ActualValueDelegate<object> testDelegate = () => _import.LoadBatchFile(path);
// Assert
Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>());
}
[测试用例(空)]
[测试用例(“”)
public void ShouldThrowArgumentException(字符串路径)
{
//表演
ActualValueDelegate testDelegate=()=>\u import.LoadBatchFile(路径);
//断言
Assert.That(testDelegate,Throws.TypeOf对于这些场景,我倾向于使用TestCase(见下文)。这样,您只需编写一次测试,并且只指定要测试的不同值。在您的情况下,为null且为空字符串
[TestCase(null)]
[TestCase("")]
public void ShouldThrowArgumentException(string path)
{
// Act
ActualValueDelegate<object> testDelegate = () => _import.LoadBatchFile(path);
// Assert
Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>());
}
[测试用例(空)]
[测试用例(“”)
public void ShouldThrowArgumentException(字符串路径)
{
//表演
ActualValueDelegate testDelegate=()=>\u import.LoadBatchFile(路径);
//断言
Assert.That(testDelegate,Throws.TypeOf测试代码与任何其他代码一样。如果抽象出某些东西是有意义的,那么就这样做。不过,在你的情况下,你赢不了多少。我认为塞德里克·罗耶·伯特兰的解决方案看起来很不错。只需添加
[TestCase(" ")]
你已经完成了。测试代码和其他代码一样。如果把某些东西抽象出来是有意义的,那么就去做。不过,在你的情况下,你赢不了多少。我认为塞德里克·罗耶·贝特朗的解决方案看起来很不错。只需添加
[TestCase(" ")]
您已经完成了。我建议您使用TestCaseSourceAttribute
,您可以将所有输入案例放在一个实现IEnumerable
的对象中,然后调用此对象的测试。因此,它看起来是这样的:
[TestCase(null)]
[TestCase("")]
public void ShouldThrowArgumentException(string path)
{
Assert.That(() => _import.LoadBatchFile(path), Throws.TypeOf<ArgumentNullException>());
}
[TestCaseSource(nameof(ShouldThrowArgumentSource))]
public void ShouldThrowArgumentException(string path)
{
// Act
ActualValueDelegate<object> testDelegate = () => _import.LoadBatchFile(path);
// Assert
Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>());
}
private static IEnumerable ShouldThrowArgumentSource()
{
yield return string.Empty;
yield return null;
}
[TestCaseSource(名称(ShouldThrowArgumentSource))]
public void ShouldThrowArgumentException(字符串路径)
{
//表演
ActualValueDelegate testDelegate=()=>\u import.LoadBatchFile(路径);
//断言
Assert.That(testDelegate,Throws.TypeOf aboutTestCaseSource
我建议您使用TestCaseSourceAttribute
,您可以将所有输入案例放在一个实现IEnumerable
的对象中,然后为此对象调用您的测试。因此,它看起来是这样的:
[TestCase(null)]
[TestCase("")]
public void ShouldThrowArgumentException(string path)
{
Assert.That(() => _import.LoadBatchFile(path), Throws.TypeOf<ArgumentNullException>());
}
[TestCaseSource(nameof(ShouldThrowArgumentSource))]
public void ShouldThrowArgumentException(string path)
{
// Act
ActualValueDelegate<object> testDelegate = () => _import.LoadBatchFile(path);
// Assert
Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>());
}
private static IEnumerable ShouldThrowArgumentSource()
{
yield return string.Empty;
yield return null;
}
[TestCaseSource(名称(ShouldThrowArgumentSource))]
public void ShouldThrowArgumentException(字符串路径)
{
//表演
ActualValueDelegate testDelegate=()=>\u import.LoadBatchFile(路径);
//断言
Assert.That(testDelegate,Throws.TypeOf aboutTestCaseSource
你是想用Assert.Throws()
还是Assert.DoesNotThrow()
?我不知道。我已经复制了Assert.That。所以回答:你不应该在同一篇文章中问两个问题。你想用Assert.Throws()吗
或Assert.DoesNotThrow()
?我不知道。我已经复制了Assert。因此,回答:你不应该在同一篇文章中问两个问题。