C#闰年测试程序白盒测试 20150426

Posted on 2015-04-26 15:09  三班&吴少博  阅读(1434)  评论(0编辑  收藏  举报

  使用visual studio对C#程序进行简单的单元测试。

  1. 测试步骤

     1). 新建闰年测试项目,编写代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     public class Program
10     {
11         static void Main(string[] args)
12         {
13 
14         }
15         public static bool leap = new bool();
16         public static bool isLeapYear(int year)
17         {
18             if (year == 0 || year < 0)
19                 return false;
20             leap = false;
21             if (year % 4 == 0)
22                 leap = true;
23             if (year % 100 == 0)
24                 leap = false;
25             if (year % 400 == 0)
26                 leap = true;
27             return leap;
28 
29         }
30     }
31 }

2).在项目解决方案管理器中新建测试项目并为项目添加引用

3).编写测试代码:

 1 using System;
 2 using Microsoft.VisualStudio.TestTools.UnitTesting;
 3 
 4 namespace UnitTestProject1
 5 {
 6     [TestClass]
 7     public class UnitTest1
 8     {
 9         [TestMethod]
10         public void TestMethod1()
11         {
12             Assert.AreEqual(ConsoleApplication1.Program.isLeapYear(2012), true );
13         }
14         [TestMethod]
15         public void TestMethod2()
16         {
17             Assert.AreEqual(ConsoleApplication1.Program.isLeapYear(2000), true);
18         }
19         [TestMethod]
20         public void TestMethod3()
21         {
22             Assert.AreEqual(ConsoleApplication1.Program.isLeapYear(2100), false);
23         }
24         [TestMethod]
25         public void TestMethod4()
26         {
27             Assert.AreEqual(ConsoleApplication1.Program.isLeapYear(0), false);
28         }
29         [TestMethod]
30         public void TestMethod5()
31         {
32             Assert.AreEqual(ConsoleApplication1.Program.isLeapYear(-4), false);
33         }
34 
35     }
36 }

4).打开测试资源管理器,生成测试

5).运行测试

2.程序流程图

3.基本路径集

A.1,2,6

B.1,2,3,6

C.1,2,3,4,6

D.1,2,3,4,5,6

4.测试用例

编号

输入用例

覆盖等价类

覆盖路径

期望结果

输出结果

Test1

2012

1,2

B

True

True

Test2

2000

1,3,5

D

True

True

Test3

2100

1,5

C

False

False

Test4

0

4

A

False

False

Test5

-1

4

A

False

False

 

 

 

 

 

 

 

 

 

测试运行成功,没有出现任何问题。