【问题标题】:Skipping Python test when dependency not available [duplicate]当依赖项不可用时跳过 Python 测试 [重复]
【发布时间】:2017-03-24 00:51:54
【问题描述】:

我正在研究一个 Python 2.7 项目,但并不真正熟悉 Python。没有安装 Firefox 时有一个测试失败,因为它使用了使用 Firefox 的 selenium。我希望测试在无法运行时自行跳过。

测试类

class SeleniumAuthTestCase(SeleniumTestCase):

我遇到的错误

Traceback (most recent call last):
  File "/mnt/vagrant/source/some/path/tests/selenium/test_auth.py", line 14, in setUpClass
    super(cls, cls).setUpClass()
  File "/mnt/vagrant/source/some/path/testcases.py", line 14, in setUpClass
    cls.driver = Firefox()
  File "/some/path/venv/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 55, in __init__
    self.binary = firefox_binary or capabilities.get("binary", FirefoxBinary())
  File "/some/path/venv/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 47, in __init__
    self._start_cmd = self._get_firefox_start_cmd()
  File "/some/path/venv/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 163, in _get_firefox_start_cmd
    " Please specify the firefox binary location or install firefox")
RuntimeError: Could not find firefox in your system PATH. Please specify the firefox binary location or install firefox

我发现有一种方法可以拥有individual test methods skipped via annotation。但是,这里的错误发生在调用任何测试方法之前:在父类中的setUpClass 中。

我还发现我可以重载该方法:

@classmethod
def setUpClass(cls):
    super(SeleniumAuthTestCase, cls).setUpClass()

所以我可以在那里检查是否加载了依赖项,如果没有加载,请避免调用父类。最重要的是,我可以设置一些标志来指示该事物是否已加载,然后为每个检查它的方法添加一个注释。不过这很笨拙,我更想做这样的 PHPUnit 代码:

public function setUp() {
    if ( true ) {
        $this->markTestSkipped();
    }
}

这通常在 Python 中是如何完成的?

【问题讨论】:

标签: python selenium testing python-unittest


【解决方案1】:

approach that sbarzowski linked 为我工作。

@classmethod
def setUpClass(cls):
    try:
        Firefox()
    except:
        raise unittest.SkipTest("Selenium webdriver needs Firefox, which is not available")

    super(SeleniumAuthTestCase, cls).setUpClass()

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2017-06-24
    • 2013-12-07
    • 2014-10-06
    相关资源
    最近更新 更多