unittest框架的注意點


這篇並不是講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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM