NUnit 3, VS2015
I want to test exception types of my method through TestCase
attributes using, but NUnit3TestAdapter
doesn't see my test in VS2015 (my class is 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));
}
Even more: at this case NUnit3TestAdapter
doesn't see my all tests... But if I comment this test then NUnit3TestAdapter
sees other tests:
// [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));
}
How can I solve it? I wouldn't like to create five separate tests...
I suspect the problem is that the test method is generic. Instead of using the generic Assert.Throws<T>
, use the overload that accepts an exception type:
[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));
}
See more on this question at Stackoverflow