[英]check if unit-test has passed
我開發了一個搜尋器,並且進行了單元測試(主要是為了驗證XPATH)。 我想在腳本執行之前運行特定的單元測試,以確保HTML結構沒有更改並且現有的XPATH仍然有效。 我不希望單元測試的輸出,只是一個標志:通過或失敗。
例如:
tests.py:
import unittest
class CrwTst(unittest.TestCase):
def test_1(self):
[..]
crawler.py
class Crawler(object):
def action_1(self):
[..]
我想像這樣工作:
if CrwTst.test_1() is True:
Crawler.action_1()
您可能會這樣做:
crawler.py
import unittest
from tests import CrwTst
if unittest.TextTestRunner().run(CrwTst('test_1')).wasSuccessful():
Crawler.action_1()
但是請注意,您可能會遇到循環導入的問題,因為您的測試可能已經依賴於Crawler
,而您要執行的操作將使Crawler
依賴於測試。 這很可能會顯示為ImportError: cannot import name CrwTst
。
要解決此問題,您可以動態導入CrwTst
。
crawler.py
import unittest
def function_that_runs_crawler():
from tests import CrwTst # Dynamically import to resolve circular ref
if unittest.TextTestRunner().run(CrwTst('test_1')).wasSuccessful():
Crawler.action_1()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.