我正在尝试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中完成的?
答案 0 :(得分:0)
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()