再给大家介绍一个分类属性[Category(string name)],利用这个分类属性,我们可以为每个方法定义类别。
[Test, Ignore("Ignore"), Category("Category A")]
public void TestAdd()
...{
Console.WriteLine("TestAdd() Begin");
expected = 12;
actual = cal.Add(a, b);
Assert.AreEqual(expected, actual);
Console.WriteLine("TestAdd() End");
}
[Test, Category("Category B")]
[Explicit]
public void TestMinus()
...{
Console.WriteLine("TestMinus() Begin");
expected = 8;
actual = cal.Minus(a, b);
Assert.AreEqual(expected, actual);
Console.WriteLine("TestMinus() End");
}
[Test, Category("Category A")]
public void TestMultiply()
...{
Console.WriteLine("TestMultiply() Begin");
expected = 20;
actual = cal.Multiply(a, b);
Assert.AreEqual(expected, actual);
Console.WriteLine("TestMultiply() End");
}
[Test, Category("Category B")]
public void TestDivide()
...{
Console.WriteLine("TestDivide() Begin");
expected = 5;
actual = cal.Divide(a, b);
Assert.AreEqual(expected, actual);
Console.WriteLine("TestDivide() End");
}
public void TestAdd()
...{
Console.WriteLine("TestAdd() Begin");
expected = 12;
actual = cal.Add(a, b);
Assert.AreEqual(expected, actual);
Console.WriteLine("TestAdd() End");
}
[Test, Category("Category B")]
[Explicit]
public void TestMinus()
...{
Console.WriteLine("TestMinus() Begin");
expected = 8;
actual = cal.Minus(a, b);
Assert.AreEqual(expected, actual);
Console.WriteLine("TestMinus() End");
}
[Test, Category("Category A")]
public void TestMultiply()
...{
Console.WriteLine("TestMultiply() Begin");
expected = 20;
actual = cal.Multiply(a, b);
Assert.AreEqual(expected, actual);
Console.WriteLine("TestMultiply() End");
}
[Test, Category("Category B")]
public void TestDivide()
...{
Console.WriteLine("TestDivide() Begin");
expected = 5;
actual = cal.Divide(a, b);
Assert.AreEqual(expected, actual);
Console.WriteLine("TestDivide() End");
}
重新生成项目,在NUnit中,我们可以看到:
这里有我们定义的两个分类,我们选中"Category A",切换回"Tests"点"Run",我们看:
只测试了我们设置的"Category A"的一个方法,另一个方法是因为我们设置了[Ignore]所以没有执行测试。
好,到这里,我们已经把NUnit主要的属性学完了,接下来的章节我们将从实例出发学习NUnit。