2017-08-01 37 views
1

我是TDD开发新手,刚开始使用Nunit 3.7.1,C#和.NET Framework 4.7进行一些测试。使用私人nunit测试不要重复我自己

我有这个测试类:

[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>()); 
    } 
} 

为了测试这个类:

public class ImportTrzlBatch 
{ 
    public object LoadBatchFile(string path) 
    { 
     if (string.IsNullOrWhiteSpace(path)) 
      throw new ArgumentNullException(nameof(path)); 

     return null; 
    } 
} 

我测试的是path不为空,空或空白。我有三种测试方法来测试三种不同输入的相同方法。

我可以使用私人测试方法不重复代码,并从这三种方法调用它与三个不同的路径?

另一个问题是,我使用的是这样的:
ActualValueDelegate<object> testDelegate =() => _import.LoadBatchFile(path);

为了测试是否是抛出一个ArgumentNullException

是否有另一种方法来测试是否抛出异常而不使用委托?

顺便说一句,我抄代码来检查它是否抛出ArgumentNullException从这个苏答案:https://stackoverflow.com/a/33897450/68571

+0

你的意思是使用'Assert.Throws <>()'或'Assert.DoesNotThrow <>()'? – kayess

+0

我不知道。我已经从这个SO复制了'Assert.That'回答:https://stackoverflow.com/a/33897450/68571 – VansFannel

+0

你不应该在同一篇文章中提出两个问题。 –

回答

1

我建议您使用TestCaseSourceAttribute,您可以将所有输入案例放入一个实现IEnumerable的对象中,然后调用此对象的测试。所以它看起来像这样:

[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; 
    } 

欲了解更多详情,您可以阅读documentationTestCaseSource

4

对于这些情况我倾向于使用测试用例(见下文)。像这样,你只写一次测试,只是指定你想测试的不同值。在你的情况下,null和空字符串。

[TestCase(null)] 
[TestCase("")] 
public void ShouldThrowArgumentException(string path) 
{ 
    // Act 
    ActualValueDelegate<object> testDelegate =() => _import.LoadBatchFile(path); 

    // Assert 
    Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>()); 
} 

,你可以直接内嵌这样的委托:

[TestCase(null)] 
[TestCase("")] 
public void ShouldThrowArgumentException(string path) 
{ 
    Assert.That(() => _import.LoadBatchFile(path), Throws.TypeOf<ArgumentNullException>()); 
} 

有关的TestCase的能力,更多的细节来看看这个文件here

0

测试代码就像任何其他代码。如果抽象的东西是有意义的,那就去做吧。不过,在你的情况下,你赢不了多少。我认为Cedric Royer-Bertrand的解决方案看起来很漂亮。只需加上

[TestCase(" ")] 

你就完成了。