簡體   English   中英

所有測試測試都執行一次設置和拆卸功能

[英]Setup and teardown functions executed once for all nosetests tests

如何對所有測試測試執行一次設置和拆卸功能?

def common_setup():
    #time consuming code
    pass

def common_teardown():
    #tidy up
    pass

def test_1():
    pass

def test_2():
    pass

#desired behavior
common_setup()
test_1()
test_2()
common_teardown()

請注意,在使用pass替換點並添加行import unittest后,存在一個類似的問題 ,答案是不能使用python 2.7.9-1,python-unittest2 0.5.1-1和python-nose 1.3.6-1 import unittest 不幸的是,我的名聲太低,無法評論。

您可以擁有模塊級別設置功能。 根據鼻子文件

測試模塊提供模塊級設置和拆卸; 為setup,teardown, teardown_module或tearDownModule定義方法設置, setup_module ,setUp或setUpModule以進行拆卸。

所以,更具體地說,對於你的情況:

def setup_module():
    print "common_setup"

def teardown_module():
    print "common_teardown"

def test_1():
    print "test_1"

def test_2():
    print "test_2"

運行測試為您提供:

$ nosetests common_setup_test.py -s -v
common_setup
common_setup_test.test_1 ... test_1
ok
common_setup_test.test_2 ... test_2
ok
common_teardown

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

您選擇的名稱無關緊要,因此setupsetup_module工作方式相同,但setup_module更清晰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM