1

I'm poking at a Python 2.7 project while not really being familiar with Python. There is a test that fails when Firefox is not installed since it uses selenium which uses Firefox. I'd like the test to skip itself when it cannot be run.

Test class

class SeleniumAuthTestCase(SeleniumTestCase):

The error I'm getting

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

I figured out that there is a way to have individual test methods skipped via annotation. However the error here occurs before any of the test methods are invoked: in setUpClass in the parent class.

I also figured out I can overload the method:

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

So I could check there if the dependency is loaded, and if it is not, avoid calling the parent class. On top of that I could set some flag to indicate if the thing is loaded, and then have a annotation for each method that checks it. This is very clumsy though, and I'd much more like to do something like this PHPUnit code:

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

How is this normally done in Python?

Jeroen De Dauw
  • 10,321
  • 15
  • 56
  • 79
  • Likely you'll need to add some `except ImportError:`, then use `@unittest.skipIf` or `unittest.TestCase.skipTest` – o11c Mar 24 '17 at 00:57
  • 1
    http://stackoverflow.com/questions/11452981/skip-unittest-if-some-condition-in-setupclass-fails – sbarzowski Mar 24 '17 at 01:09

1 Answers1

0

The approach that sbarzowski linked worked for me.

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

    super(SeleniumAuthTestCase, cls).setUpClass()
Community
  • 1
  • 1
Jeroen De Dauw
  • 10,321
  • 15
  • 56
  • 79