2017-04-12 72 views
0

NUnit的3,VS2015
我想用通过TestCase属性测试异常类型我的方法,但NUnit3TestAdapter没有看到我在VS2015的测试(我的课是public):如何通过一次测试来测试我的方法异常集?

[TestCase(null, typeof(ArgumentNullException))] 
[TestCase("", typeof(ArgumentException))] 
[TestCase(" ", typeof(ArgumentException))] 
[TestCase(" \t \t ", typeof(ArgumentException))] 
[TestCase("< !!!>", typeof(FileNotFoundException))] 
public void InvalidFilePath_ThrowsException<T>(string name, 
    T exception_type) where T : Exception { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<T>(() => ModelFactory.Current.CreateDocument(name, 
     dict)); 
} 

更:在这种情况下NUnit3TestAdapter没有看到我所有的测试......但如果我评论这个测试,然后NUnit3TestAdapter看到其他检查:

// [TestCase(null, typeof(ArgumentNullException))] 
// [TestCase("", typeof(ArgumentException))] 
// [TestCase(" ", typeof(ArgumentException))] 
// [TestCase(" \t \t ", typeof(ArgumentException))] 
// [TestCase("<!!!>", typeof(FileNotFoundException))] 
// public void InvalidFilePath_ThrowsException<T>(string name, 
// T exception_type) where T : Exception { 

// Dictionary<string, string> dict = new Dictionary<string, string>(); 

// Assert.Throws<T>(() => ModelFactory.Current.CreateDocument(name, 
//  dict)); 
//} 

[Test] 
public void InvalidFilePath_ThrowsException_01() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<ArgumentNullException>(() => ModelFactory.Current 
    .CreateDocument(null, dict)); 
} 

[Test] 
public void InvalidFilePath_ThrowsException_02() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<ArgumentException>(() => ModelFactory.Current 
    .CreateDocument("", dict)); 
} 

[Test] 
public void InvalidFilePath_ThrowsException_03() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<ArgumentException>(() => ModelFactory.Current 
    .CreateDocument(" ", dict)); 
} 

[Test] 
public void InvalidFilePath_ThrowsException_04() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<ArgumentException>(() => ModelFactory.Current 
    .CreateDocument(" \t \t ", dict)); 
} 

[Test] 
public void InvalidFilePath_ThrowsException_05() { 

    Dictionary<string, string> dict = new Dictionary<string, string>(); 

    Assert.Throws<FileNotFoundException>(() => ModelFactory.Current 
    .CreateDocument("<!!!>", dict)); 
} 

我怎样才能解决呢?我不想创建五个单独的测试...

+0

看这个问题:http://stackoverflow.com/questions/801153/parametric-test-with-generic-methods – vyrp

+0

如何该信息可以帮助我在我的情况? –

+0

发布了一个答案 – vyrp

回答

1

我怀疑问题是测试方法是通用的。而不是使用通用Assert.Throws<T>的,使用接受的异常类型的过载:

[TestCase(null, typeof(ArgumentNullException))] 
[TestCase("", typeof(ArgumentException))] 
[TestCase(" ", typeof(ArgumentException))] 
[TestCase(" \t \t ", typeof(ArgumentException))] 
[TestCase("< !!!>", typeof(FileNotFoundException))] 
public void InvalidFilePath_ThrowsException(string name, Type exceptionType) 
{ 
    Dictionary<string, string> dict = new Dictionary<string, string>();  
    Assert.Throws(exceptionType,() => ModelFactory.Current.CreateDocument(name, dict)); 
} 
+0

很好的答案,但令人惊讶的是,通用版本无法正常工作。您可以尝试颠倒参数,以便首先出现异常类型。我很想知道会发生什么。 – Charlie

+0

@Charlie:当我有时间拨弄时,必须要拍摄一张... –

+0

啊!没关系。这不是泛型的有效用法,因为该方法使用Type而不是异常的实例。结果是一个容易犯的错误。 :-) – Charlie

相关问题