如何通过一次测试来测试我的方法异常集?

时间:2017-04-12 09:14:39

标签: c# nunit nunit-3.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));
}

我该如何解决?我不想创建五个单独的测试...

1 个答案:

答案 0 :(得分: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));
}