在大型项目开发过程中,软件开发和测试是同步进行的。程序代码由软件开发人员编写,而测试代码由测试工程师编写,因此这两种代码应该分别保存在不同的类文件中,以便统一管理,下面举一例。
1、创建C#类库,名为CompareMaxNumber。
2、创建C#类文件,名为LargestNumber.cs。此代码由软件开发人员编写,用于判定list列表中的最大值。
using System; using System.Collections.Generic; using System.Text; namespace CompareMaxNumber { public class LargestNumber { public static int MaxNumber(int[] list) { int max = Int32.MinValue; for (int i = 0; i < list.Length; i++) { if (list[i] > max) { max = list[i]; } } return max; } } }
3、创建C#类文件,名为TestLargestNumber.cs。此代码由软件测试人员编写,用于测试LargestNumber.cs中的逻辑。
using System; using System.Collections.Generic; using System.Text; using NUnit.Framework; namespace CompareMaxNumber { [TestFixture] class TestLargestNumber { [Test] public void TestLargest() { Assert.AreEqual(9,LargestNumber.MaxNumber(new int[] {8, 7, 9})); } } }
4、设置类库的启动操作,将启动外程序的路径设置为nunit.,exe。启动调试,点击run,得到运行结果。