我想创建NUnit测试以确保我的函数不会抛出异常.是否有一些特定的方法来做,或者我应该写
[Test]
public void noExceptionTest() {
testedFunction();
}
Run Code Online (Sandbox Code Playgroud)
如果没有抛出异常,它会成功吗?
让我们想象一下,我想把三个文件连续地传递给一个用户,但不是他把一个Stream对象交给我推送字节,我必须给他一个Stream他将从中提取字节的对象.我想拍摄我的三个FileStream对象(甚至更聪明,一个IEnumerable<Stream>)并返回一个新的ConcatenatedStream对象,它将根据需要从源流中提取.
我正在尝试使用a中的ExpectedException属性C# UnitTest,但我遇到问题让它与我的特定工作Exception.这是我得到的:
注意:我在线路周围包裹了星号,这给我带来了麻烦.
[ExpectedException(typeof(Exception))]
public void TestSetCellContentsTwo()
{
// Create a new Spreadsheet instance for this test:
SpreadSheet = new Spreadsheet();
// If name is null then an InvalidNameException should be thrown. Assert that the correct
// exception was thrown.
ReturnVal = SpreadSheet.SetCellContents(null, "String Text");
**Assert.IsTrue(ReturnVal is InvalidNameException);**
// If text is null then an ArgumentNullException should be thrown. Assert that the correct
// exception was thrown.
ReturnVal = SpreadSheet.SetCellContents("A1", (String) null);
Assert.IsTrue(ReturnVal …Run Code Online (Sandbox Code Playgroud) 我正在进行单元测试的第一步,并编写了(以及其他)这两种方法:
[TestCase]
public void InsertionSortedSet_AddValues_NoException()
{
var test = new InsertionSortedSet<int>();
test.Add(5);
test.Add(2);
test.Add(7);
test.Add(4);
test.Add(9);
}
[TestCase]
public void InsertionSortedSet_AddValues_CorrectCount()
{
var test = new InsertionSortedSet<int>();
test.Add(5);
test.Add(2);
test.Add(7);
test.Add(4);
test.Add(9);
Assert.IsTrue(test.Count == 5);
}
Run Code Online (Sandbox Code Playgroud)
NoException真的需要这种方法吗?如果要抛出异常,它也将被抛出CorrectCount.
我倾向于将它保留为2个测试用例(可能将重复的代码重构为另一种方法),因为测试应该只测试单个事物,但也许我的解释是错误的.