经伟

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

这篇并不是讲unittest如何使用,而是记录下在和htmltestrunner集成使用过程中遇到的一些坑,主要是报告展示部分。

我们都知道python有一个单元测试框架pyunit,也叫unittest,类似于java的junit。功能也比较丰富,他也有初始函数setUp(self), 清理函数tearDown(self)。

它有两只执行方式:

一、使用时首先我们的测试类要继承于unittest.TestCase.  然后把我们的测试都放到unittest.TestSuite()容器中,最后使用 unittest.TextTestRunner().run(suite)方法自动测试。

二、编写的测试用例类继承unittest.TestCase类,所有测试函数以test开头,执行时,执行unittest.main(),所有测试类中以test开头的函数自动执行。

 

下面是我的一个实验代码,在加上了断言后遇到一些坑,或者说是因为自己对unitest不熟悉而带来的误解吧。

testsuite.addTest(testClass("testsearch2")),有这一行可知我只加入了testsearch2用例到testsuite,那么按道理来讲,不论运行结果是pass还是fail还是异常,setup和teardown都应该只被执行一次。

但实际情况是,在运行完后看report时,report的结果会带来一些误解:

1、如果pass,report结果显示很准确,

2、但如果运行中出现异常,report会显示成这样,单看这个report,会有一种错觉就是setup被执行了2次,第一次是在testsearch2运行时,第二次是teardown运行时,但是从逻辑来讲,setup和teardown只应该被执行一次,所以我定义了一个变量,并且在setup里自增,就是setup每调用一次就自增一并且打印出来,从下面的log可以看出,虽然报告里是显示了2个testsearch2的运行错误,但是自增变量的值都是1,由此可以推测setup其实还是只被运行了一次,应该是HTMLTestRunner报告的展示问题而引起的误解。所以这个也是后期在进行设计时要注意的,需要对报告进行封装以避免  误解。

 

# coding = utf-8
import time
import unittest
import HTMLTestRunner
import os
import sys
from selenium import webdriver

class test():
    def add(self, x,y):
        return x+y

class testClass(unittest.TestCase):
    count=0
    count_=0
    def setUp(self):
        self.count += 1
        print "setup...",self.count
        #self.driver=webdriver.Firefox()
        #time.sleep(3)
        #self.driver.get("https://www.baidu.com")
        #time.sleep(3)


    def verifyEquals(self,exp,act,msg):
        try:
            self.assertEquals(exp,act,msg)
            print 'assertion passed ',msg
        except:
            print 'catch exception here ',msg

    #assert in unittest will just show the msg content when it is failed, you can see its source code, so for pass situation, if also want to show msg content, need to write code yourself.
    def testsearch2(self):

        self.verifyEquals('123','1234','equals')
        self.assertIn("123","1234","assert in ---")

        '''
        time.sleep(30)
        input=self.driver.find_element_by_id('kw')
        search=self.driver.find_element_by_id('su')
        input.send_keys("byebye")
        search.click()
        self.assertIn(self, "123","1234","assert in ---")
        '''

    def testsearch(self):
        input=self.driver.find_element_by_id('kw')
        search=self.driver.find_element_by_id('su')
        input.send_keys("hello")
        search.click()
        print "assertion"

        self.assertTrue(search.is_displayed(),"baidu yixia should display")

    def tearDown(self):
        self.count +=1
        print 'test down...',self.count
        #self.driver.quit()
        #self.driver.close()

if __name__ == '__main__':
    #unittest.main()
    #unittest.TestCase.assertTrue()

    a=test()
    print a.add(2,4)

    '''
    vp=testClass()
    vp.verifyEquals("123",'234','check equals or not')
    '''

    current_path=os.getcwd()
    print 'current path: ',current_path
    project_path=os.path.dirname(current_path)
    print "project path:",project_path

    testsuite=unittest.TestSuite()
    testsuite.addTest(testClass("testsearch2"))
    #testsuite.addTest(testClass("testsearch"))
    temp=str(time.time())

    filedir=project_path+"//report//"+temp
    os.makedirs(filedir)
    filename="//pyresult.html"
    filepath=filedir+filename
    fp=file(filepath,'wb')
    runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title='report',description='demo')
    runner.run(testsuite)

 

posted on 2016-05-14 09:39  经伟  阅读(5034)  评论(0编辑  收藏  举报