简体   繁体   English

检查单元测试是否通过

[英]check if unit-test has passed

I developed a crawler and it's unit-tests (mainly to validate XPATHs). 我开发了一个搜寻器,并且进行了单元测试(主要是为了验证XPATH)。 I want to run specific unit-tests before script execution in order to be sure that HTML structure has not changed and existing XPATHs still working. 我想在脚本执行之前运行特定的单元测试,以确保HTML结构没有更改并且现有的XPATH仍然有效。 I don't want the output of unit-test, just a flag: passed or failed. 我不希望单元测试的输出,只是一个标志:通过或失败。

for example: 例如:

tests.py: tests.py:

import unittest

class CrwTst(unittest.TestCase):
    def test_1(self):
        [..]

crawler.py crawler.py

class Crawler(object):
    def action_1(self):
        [..]

and I want to work like: 我想像这样工作:

if CrwTst.test_1() is True:
    Crawler.action_1()

You could potentially do this: 您可能会这样做:

crawler.py crawler.py

import unittest
from tests import CrwTst

if unittest.TextTestRunner().run(CrwTst('test_1')).wasSuccessful():
    Crawler.action_1()

Note however that you may run into an issue with circular imports, because your test presumably already depends on Crawler , and what you are looking to do will make the Crawler depend on the test. 但是请注意,您可能会遇到循环导入的问题,因为您的测试可能已经依赖于Crawler ,而您要执行的操作将使Crawler依赖于测试。 This will likely manifest itself as ImportError: cannot import name CrwTst . 这很可能会显示为ImportError: cannot import name CrwTst

To resolve that, you can dynamically import the CrwTst . 要解决此问题,您可以动态导入CrwTst

crawler.py 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM