2017-08-01 68 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(" ")] 

你就完成了。