Skip to content

Commit

Permalink
Consistently stash the base URL in the configuration options
Browse files Browse the repository at this point in the history
  • Loading branch information
davehunt committed Dec 3, 2015
1 parent b00481b commit 70422d4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
17 changes: 17 additions & 0 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ responding before proceeding with the test suite. To enable this, specify the
``--verify-base-url`` command line option or set the ``VERIFY_BASE_URL``
environment variable to ``TRUE``.

Skipping Base URLs
------------------

You can `skip tests <http://pytest.org/latest/skipping.html>`_ based on the
value of the base URL so long as it is provided either by the command line or
in a configuration file:

.. code-block:: python
import pytest
@pytest.mark.skipif('dev' in pytest.config.getoption('base_url'), reason='Search not available on dev')
def test_search(base_url, selenium):
selenium.get('{0}/search'.format(base_url))
Unfortunately if the URL is provided by a fixture, there is no way to know this
value at test collection.

Sensitive Environments
**********************
Expand Down
7 changes: 5 additions & 2 deletions pytest_selenium/pytest_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def _environment(request, capabilities):
def base_url(request):
"""Return a base URL"""
config = request.config
base_url = config.option.base_url or config.getini('base_url')
if base_url:
base_url = config.getoption('base_url')
if base_url is not None:
config._environment.append(('Base URL', base_url))
return base_url

Expand Down Expand Up @@ -81,6 +81,9 @@ def selenium(request, capabilities):
def pytest_configure(config):
if hasattr(config, 'slaveinput'):
return # xdist slave
base_url = config.getoption('base_url') or config.getini('base_url')
if base_url is not None:
config.option.base_url = base_url
config.addinivalue_line(
'markers', 'capabilities(kwargs): add or change existing '
'capabilities. specify capabilities as keyword arguments, for example '
Expand Down
26 changes: 24 additions & 2 deletions testing/test_base_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,29 @@ def test_config(testdir, webserver):
file_test = testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_config(base_url):
def test_config(request, base_url):
assert request.config.getvalue('base_url') == '{0}'
assert request.config.getini('base_url') == '{0}'
assert base_url == '{0}'
""".format(base_url))
testdir.inline_run(file_test)
reprec = testdir.inline_run(file_test)
passed, skipped, failed = reprec.listoutcomes()
assert len(passed) == 1


def test_skip_config(testdir, webserver):
base_url = 'http://localhost:{0}/foo'.format(webserver.port)
testdir.makefile('.ini', pytest="""
[pytest]
base_url={0}
""".format(base_url))
file_test = testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
@pytest.mark.skipif(pytest.config.getoption('base_url') == '{0}',
reason='skip')
def test_skip_config(): pass
""".format(base_url))
reprec = testdir.inline_run(file_test)
passed, skipped, failed = reprec.listoutcomes()
assert len(skipped) == 1

0 comments on commit 70422d4

Please sign in to comment.