zoukankan      html  css  js  c++  java
  • 单元测试的运用

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

    白盒测试:又称结构测试,它一般用来测试程序的内部结构(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 }

  • 相关阅读:
    0705. Design HashSet (E)
    VMware简单使用
    Git笔记
    初识MyBatis
    数据库连接池配置 testOnBorrow
    Redis list操作命令
    文件/目录对比:diff命令
    可用于区块链的共识算法
    分布式一致性算法,你确定不了解一下?
    Jmeter文件下载测试
  • 原文地址:https://www.cnblogs.com/panbosponge/p/4454807.html
Copyright © 2011-2022 走看看