码迷,mamicode.com
首页 > 其他好文 > 详细

单元测试的运用

时间:2015-04-24 23:57:14      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

上个星期做了有关白盒测试的测试,何为“白盒测试”,

白盒测试:又称结构测试,它一般用来测试程序的内部结构(Control Flow , Data Flow)。并判定其结果是否与预期的结果一致。
白盒测试的种类:静态分析测试(Static Analysis Test,Code Inspection)、语句分支覆盖测试(Ctrl  Flow Test)等。
 这次实验一开始大家都不知道如何下手,于是,我便在网上参照了教材对一个简单的APP进行了白盒测试。
 

测试项目名称:闰年测试器

版本号:1.0

测试项目介绍:通过输入年份判断该年是否为闰年,便于人们使用

测试目的:测试该程序判定条件是否正确,是否考虑到非法输入的情况

测试时间:2014年4月20日

测试人员:潘博

测试环境:windows8+visual studio2013

 

操作步骤:

1.在解决方案资源管理器中,选择解决方案名称,从快捷菜单中选择“添加”,然后选择“新建项目”。 在“新建项目”对话框中,展开“已安装”、“Visual C#”,然后选择“Windows 应用商店”。 然后从项目模板列表中选择“单元测试库(Windows Store 应用程序)”

技术分享

2. 在 Visual Studio 编辑器中打开 UnitTest1.cs。

技术分享

3.在 UnitTest1.cs 文件的 TestMethod1 中插入一些测试代码

验证测试是否在测试资源管理器中运行

技术分享


4.在“测试”菜单上,选择“运行”,然后选择“全部运行”。

技术分享

 

5.将对 App4应用程序的引用添加到 RooterTests 项目。

  1. 在解决方案资源管理器中,选择“RooterTests”项目,然后选择快捷菜单上的“添加引用...”。
  2. 在“添加引用 - RooterTests”对话框上,展开“解决方案”,再选择“项目”。 然后选择“App4”项目。

技术分享

6.向 UnitTest1.cs 文件添加 using 语句:

  1. 打开 UnitTest1.cs

      2.在 using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; 行下添加以下代码:

技术分享

7.添加使用 Rooter 函数的测试。 将下列代码添加到 UnitTest1.cpp

技术分享

8.生成解决方案。

新测试将显示在测试资源管理器的“未运行的测试”节点中。

在测试资源管理器中,选择“全部运行”。

技术分享

测试用例:

用例编号

输入

预期输出

实际输出

覆盖路径

是否通过测试

1

2000

该年是闰年

该年是闰年

1-2-4-5-6

通过

2

2001

该年不是闰年

该年不是闰年

1-2-4-5-7

通过

3

1000

该年不是闰年

该年不是闰年

1-2-4-5-7

通过

4

一九二五

输入有误

输入有误

1-2-3

通过

5

Abcd

输入有误

输入有误

1-2-3

通过

输出结果:

技术分享

技术分享技术分享

技术分享

技术分享

测试源码:

MainPage.xaml.cs

1

 2 using System;

 3 using System.Collections.Generic;

 4 using System.IO;

 5 using System.Linq;

 6 using System.Runtime.InteropServices.WindowsRuntime;

 7 using Windows.Foundation;

 8 using Windows.Foundation.Collections;

 9 using Windows.UI.Xaml;

10 using Windows.UI.Xaml.Controls;

11 using Windows.UI.Xaml.Controls.Primitives;

12 using Windows.UI.Xaml.Data;

13 using Windows.UI.Xaml.Input;

14 using Windows.UI.Xaml.Media;

15 using Windows.UI.Xaml.Navigation;

16

17

18

19

20

21 // ???é???é????? http://go.microsoft.com/fwlink/?LinkId=234238 ä???ä???

22

23 namespace App4

24 {

25     /// <summary>

26     /// ??ä??è??è????è??è?? Frame ?é?????é???

27     /// </summary>

28     public sealed partial class MainPage : Page

29     {

30         public MainPage()

31         {

32             this.InitializeComponent();

33         }

34

35         private void TextBox_TextChanged(object sender, TextChangedEventArgs e)

36         {

37         }

38         private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e)

39         {

40

41         }

42

43         private void Button_Click(object sender, RoutedEventArgs e)

44         {

45             string year = textbox.Text;

46             

47

48             Rooter rr = new Rooter();

49             textbox1.Text = rr.isleap(year);

50

51            

52                

53         }

54            

55         }

56          

57

58         }

59     

60

Rooter.cs

1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 using System.Threading.Tasks;

 6

 7

 8 namespace App4

 9 {

10     public class Rooter

11     {

12         public Rooter()

13         {

14         }

15          public string isleap(string year)

16              

17         {    string shuchu;

18             try

19             {

20                 int year1 = Int32.Parse(year);

21

22                 if ((year1 % 4 == 0 && year1 % 100 != 0) || year1 % 400 == 0)

23                 {

24                     shuchu = "è????é???";

25

26                 }

27                 else

28                 {

29                     shuchu = "è???ä???é???";

30

31                 }

32             }

33             catch (FormatException)

34             {

35                 shuchu = "è????è??";

36                 

37                 

38             }

39             return shuchu;

40            

41         }

42     }

43

44 }

UnitTest1.cs

1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;

 6 using App4;

 7

 8 namespace UnitTestLibrary1

 9 {

10     [TestClass]

11     public class UnitTest1

12     {

13         [TestMethod]

14         public void TestMethod1()

15         {

16             Rooter rr = new Rooter();

17             String year = "2000";

18             String shuchu = rr.isleap(year);

19             String expect = "è????é???";

20             Assert.AreEqual(shuchu, expect);

21         }

22

23     }

24 }

单元测试的运用

标签:

原文地址:http://www.cnblogs.com/panbosponge/p/4454807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!