Skip to content

Commit

Permalink
Disable base URL verification by default
Browse files Browse the repository at this point in the history
  • Loading branch information
davehunt committed Aug 25, 2015
1 parent 45f31b3 commit 4e2f587
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 40 deletions.
13 changes: 12 additions & 1 deletion docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,24 @@ may be able to take advantage of one of the following:
`pytest-flask <http://pytest-flask.readthedocs.org/>`_, which provides a
``live_server`` fixture.

Verifying the Base URL
----------------------

If you specify a base URL for a site that's unavailable then all tests using
that base URL will likely fail. To avoid running every test in this instance,
you can enable base URL verification. This will check the base URL is
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``.


Sensitive Environments
**********************

To avoid accidental changes being made to sensitive environments such as
your production instances, all tests are assumed to be destructive. Any
destructive tests attempted to run against a sensitive environment will be
skipped. By default, all URLs are considered to be sensitive.
skipped.

Nondestructive Tests
--------------------
Expand Down
29 changes: 17 additions & 12 deletions pytest_selenium/pytest_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ def _environment(request, capabilities):

@pytest.fixture(scope='session')
def base_url(request):
"""Return a verified base URL"""
"""Return a base URL"""
config = request.config
base_url = config.option.base_url or config.getini('base_url')
if base_url:
verify_url(base_url)
if hasattr(config, '_html'):
config._html.environment.append(('Base URL', base_url))
raise Exception('test')
return base_url


def verify_url(url):
response = requests.get(url, timeout=REQUESTS_TIMEOUT)
ok_codes = (200, 401)
if response.status_code not in ok_codes:
raise pytest.UsageError(
'Base URL did not respond with one of the following status '
'codes: {0}.\nURL: {1},\nResponse status code: {2.status_code}'
'\nResponse headers: {2.headers}'.format(
', '.join(map(str, ok_codes)), url, response))
@pytest.fixture(scope='session', autouse=True)
def _verify_url(request, base_url):
"""Verifies the base URL"""
verify = request.config.option.verify_base_url
if base_url and verify:
response = requests.get(base_url, timeout=REQUESTS_TIMEOUT)
if not response.status_code == requests.codes.ok:
raise pytest.UsageError(
'Base URL failed verification!'
'\nURL: {0}, Response status code: {1.status_code}'
'\nResponse headers: {1.headers}'.format(base_url, response))


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -171,6 +171,11 @@ def pytest_addoption(parser):
group._addoption('--base-url',
metavar='url',
help='base url for the application under test.')
group._addoption('--verify-base-url',
action='store_true',
default=not os.getenv(
'VERIFY_BASE_URL', 'false').lower() == 'false',
help='verify the base url.')
group._addoption('--host',
default=os.environ.get('SELENIUM_HOST', 'localhost'),
metavar='str',
Expand Down
27 changes: 0 additions & 27 deletions testing/test_base_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
pytestmark = pytest.mark.nondestructive


def failure_with_output(testdir, *args, **kwargs):
reprec = testdir.inline_run(*args, **kwargs)
passed, skipped, failed = reprec.listoutcomes()
assert len(failed) == 1
out = failed[0].longrepr.reprcrash.message
return out


def test_fixture(testdir):
file_test = testdir.makepyfile("""
import pytest
Expand Down Expand Up @@ -51,22 +43,3 @@ def test_config(base_url):
assert base_url == '{0}'
""".format(base_url))
testdir.inline_run(file_test)


def test_failing_base_url(testdir, webserver):
status_code = 500
base_url = 'http://localhost:{0}/{1}/'.format(webserver.port, status_code)
testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_pass(): pass
""".format(base_url))
result = testdir.runpytest('--base-url', base_url)
assert result.ret != 0
# tracestyle is native by default for hook failures
result.stdout.fnmatch_lines([
'*UsageError: Base URL did not respond with one of the following '
'status codes: *.',
'*URL: {0}*'.format(base_url),
'*Response status code: {0}*'.format(status_code),
'*Response headers: {*}*'])
58 changes: 58 additions & 0 deletions testing/test_verify_base_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import pytest

pytestmark = pytest.mark.nondestructive


@pytest.fixture
def testfile(testdir):
return testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_pass(): pass
""")


def failure_with_output(testdir, *args, **kwargs):
reprec = testdir.inline_run(*args, **kwargs)
passed, skipped, failed = reprec.listoutcomes()
assert len(failed) == 1
out = failed[0].longrepr.reprcrash.message
return out


def test_ignore_bad_url(testdir, testfile, webserver):
base_url = 'http://localhost:{0}/500/'.format(webserver.port)
testdir.quick_qa('--base-url', base_url, testfile, passed=1)


def test_enable_verify_via_cli(testdir, testfile, webserver, monkeypatch):
monkeypatch.setenv('VERIFY_BASE_URL', False)
status_code = 500
base_url = 'http://localhost:{0}/{1}/'.format(webserver.port, status_code)
out = failure_with_output(testdir, testfile, '--base-url', base_url,
'--verify-base-url')
assert 'UsageError: Base URL failed verification!' in out
assert 'URL: {0}'.format(base_url) in out
assert 'Response status code: {0}'.format(status_code) in out
assert 'Response headers: ' in out


def test_enable_verify_via_env(testdir, testfile, webserver, monkeypatch):
monkeypatch.setenv('VERIFY_BASE_URL', True)
status_code = 500
base_url = 'http://localhost:{0}/{1}/'.format(webserver.port, status_code)
out = failure_with_output(testdir, testfile, '--base-url', base_url)
assert 'UsageError: Base URL failed verification!' in out
assert 'URL: {0}'.format(base_url) in out
assert 'Response status code: {0}'.format(status_code) in out
assert 'Response headers: ' in out


def test_disable_verify_via_env(testdir, testfile, webserver, monkeypatch):
monkeypatch.setenv('VERIFY_BASE_URL', False)
base_url = 'http://localhost:{0}/500/'.format(webserver.port)
testdir.quick_qa('--base-url', base_url, testfile, passed=1)

0 comments on commit 4e2f587

Please sign in to comment.