如何仅通过一次测试来测试我的方法异常集?
How can I test the set of my method exceptions through one test only?
N单元 3,VS2015
我想通过 TestCase
属性使用来测试我的方法的异常类型,但是 NUnit3TestAdapter
在 VS2015 中看不到我的测试(我的 class 是 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));
}
我该如何解决?我不想创建五个单独的测试...
我怀疑问题是测试方法是通用的。不要使用泛型 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));
}
N单元 3,VS2015
我想通过 TestCase
属性使用来测试我的方法的异常类型,但是 NUnit3TestAdapter
在 VS2015 中看不到我的测试(我的 class 是 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));
}
我该如何解决?我不想创建五个单独的测试...
我怀疑问题是测试方法是通用的。不要使用泛型 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));
}