首页 > 技术文章 > 我的单元测试之总结

haining1993 2016-02-01 17:05 原文

单元测试

版权声明:本文为博主原创文章,未经博主允许不得转载。

以下关于单元测试的总结,是基于目前工作的内容进行的汇总,包括了单元测试的定义,单元测试assertion语句,单元测试的框架以及实践中的注意事项等。其中【***】为解释说明。在此推荐几本有关单元测试的书籍供参考。《单元测试的艺术》《单元测试之道junit(Java版)》《单元测试之道Nunit(C#版)》。

Overview

一个UT当中,包括了准备数据,释放资源,执行要验证的那段逻辑代码,以及结果的验证等。

准备数据:包括setUpTestCase 和setUp【setUpTestCase different from setup, setUpTestCase is Sets up test environment for test case, it should be run only one time, setup should be run first when run every test case.】setUpTestCase的数据准备是对一个UT中的所有test case所做的数据准备,这个在运行UT时系统仅运行一次,而对于setUp,每跑一个test case,都会跑一次setUp。

释放资源:包括tearDownTestCase和tearDown【When finished test case, release source, tearDownTestCase is deferent from teardown, one for all test case, one for every test case.】,一个是对整个UT运行结束后释放资源,一个是每跑完一个test case,立即释放资源,这样做的好处是防止了数据不干净产生的结果验证不正确的困扰。

执行要验证的那段逻辑代码:则我们需要写单元测试的部分是希望在系统薄弱的地方找出更多的bug,所以,开发的代码中没有逻辑的代码,我们不需要花时间去验证。应该把时间和精力放在可能出现问题的地方。

结果的验证:在准备数据的时候,我们其实已经知道了代码运行之后的结果是什么,我们只需要知道它是否跟我们的期望是一致的,跟产品的功能是一致的。还有不要为了测试而测试,case跑通了不重要,重要的是你是不是测到点上了。

准备不同的数据去尽可能的验证所有的分支,保证测试的覆盖率。

综上所述,一个UT的执行顺序是:setUpTestCase ----> setup----> testCase1---->teardown----> setup----> testCase2---->teardown ...----> tearDownTestCase .

基础知识铺垫:

一、什么是UT:

UT 指单元测试,是为了证明某段代码行为确实和开发所期望的是一致的,验证代码的功能及操作特性,换句话说就是用一段代码验证另一端代码的逻辑正确与否。

二、UT中的Assertion语句:

①验证实际值和期望值是否一致:

assertEquals(expected, actual, string message);

asserNotEquals(expected, actual, string message);

          

②验证一个给定的object是否为空或者非空:

assertNull(Object object, string message);

assertNotNull(Object object, string message);

 

③验证期望值和实际值所引用的对象是否为相同的对象:

assertSame(expected, actual, string message);

assertNotSame(expected, actual, string message);

 

④验证在条件下是否为ture或false:

assertTure(anytype condition, string message);

assertFalse(anytype condition, string message);

 

⑤验证期望的object和实际的object是否为相等的。

assertObjectEquals(expected, actual, string message);

assertNotObjectEquals(expected, actual, string message);

⑥Checks whether two values are equal with regard to a delta.

assertRealEquals(expected, actual, delta, string message);

 

⑦Checks whether two UTC date time values are equal with regard to a delta.

assertUTCDateTimeEquals(expected, actual, string message, delta)

 

三、Attribute we used

Use [SysTestGranularityAttribute(SysTestGranularity::Component)]​ ​ attribute to mark intentional component tests.
Use [SysTestGranularityAttribute(SysTestGranularity::Integration)]​ ​ attribute to mark intentional Integration tests.
Use [SysTestGranularityAttribute(SysTestGranularity::BusinessCycle)]​ ​ attribute to mark intentional Business Cycle tests.

[SysTestInactiveTest] attribute to ignore the case.

 

[SysTestMethod] attribute for each test, it can be make case can be run.

 

[SysTestCheckInTest] attribute for each test or test class.

 

In SetupTestCase for all tests in the class, add attribute to the class :

#isoCountryRegionCodes

SysTestCaseCountryRegionDependency(#isoTH)] to set country region code for test case.

 

 

[SysTestTarget(classstr(CustVendTransData), UtilElementType::Class) attribute to specify the target element for code coverage.

 

[SysProductAreaAttribute("***")] attribute to define the product area the class belongs to.

 

 

四、Framework of UT

[attribute]

class ClassName extends class

{

    Declare variable;【Use const to declare variable.】

    /// <summary>

    /// Sets up test environment for test case.

    /// </summary>

    Public void setUpTestCase()

    {

        ttsbegin;   【Begin transaction, and commit transaction or abort transaction, to prevent data is not clean】

        Prepare Data;

    ttscommit;

    }

    public void tearDownTestCase()

    {

        infolog.clear(0);

    testData.tearDown();

        testCompany.tearDown();

        super();

    }

    /// <summary>

    /// Sets up test.

    /// </summary>

    public void setUp()

    {

 

  }

 

public void tearDown()

    {

        super();

    }

    /// <summary>

    /// Comments.

    /// </summary>

    [SysTestMethod]

    public void testcase1()    【testcase1 should be use a meaningful name, and lower first letter.】

    {

        // Arrange

        [Setup the test case]

        // Act

        [Act on the system under test/unit]

        // Assert

        [Validate the result]

       }

    /// <summary>

    /// Comments.      【Xml document should be add to every test method, capital letters and summary should be a meaningful sentence.

    /// </summary>

    [SysTestMethod]

    public void testcase2()

    {

        // Arrange

        [Setup the test case]

        // Act

        [Act on the system under test/unit]

         // Assert

        [Validate the result]

    }

   

}

 

五、Tips of UT

  1. Tests should include three comments separating sections of the code:

      // Arrange

       [Setup the test case]

       // Act   

                    [Act on the system under test/unit]

            // Assert

                [Validate the result]

  2.Add ttsbegin/ttscommit/ttsabort 来进行事务处理,保证数据库不包括不完整的操作结果。

    Ttsbegin-------开始事务处理,
    Ttscommit----提交事务处理,
    Ttsabort-------回滚事务处理

    只有全部语句都成功执行后,事务处理才算成功;
  若其中有一个语句执行失败,则整个处理就算失败,
  并恢复到处理前的状态。

  3.Do ensure a test has at least one assertion or expected exception.

  4.Use data provider if you use existing data in demo data.

  5.在准备数据时,关于插入的数据是自己insert进去的,至于把什么值插入到哪张表里面是根据class中的分支,找到的依据,以及表和表的关系,如果有query,在query中找到的依据,进行总结,总结出共有多少种测试点,尽可能的覆盖所有的分支情况,从而将测试点写成代码UT,从而验证代码的逻辑是否符合功能需求。

 

 

                                                              

 

                                                               博主:海宁

联系:whnsspu@163.com

 

 


 

推荐阅读