准备
- 安装Test Framework包,导入Samples包
- 打开文档学习
Exercise 1:Runing Tests
- 打开测试窗口:Window->General->Test Runner
- 创建测试程序集:右键Create->Testing->Tests Assembly Folder
- 创建测试脚本:Create > Testing > C# test script
- 测试程序集添加引用
待测试脚本
namespace MyExercise_1
{
public static class MyMath
{
public static int Add(int a, int b)
{
return a + b;
}
//wrong
public static int Subtract(int a, int b)
{
return a - b + a; // The code is wrong. Let's see if your test can see that.
}
//correct
public static int Subtract2(int a,int b)
{
return a - b;
}
}
}
测试脚本
using NUnit.Framework;
namespace Tests
{
public class NewTestScript
{
[Test]
public void NewTestScriptSimplePasses()
{
//参数一 期望的结果
//参数二 真实的结果
Assert.AreEqual(3, MyExercise_1.MyMath.Add(1, 2));
}
[Test]
public void NewTestScriptSimplePasses2()
{
Assert.AreEqual(1, MyExercise_1.MyMath.Subtract(2, 1));
}
[Test]
public void NewTestScriptSimplePasses3()
{
Assert.AreEqual(1, MyExercise_1.MyMath.Subtract2(2, 1));
}
}
}
测试
选择一个测试,右键Run即可;测试通过,显示绿色;测试失败,显示红色。