Skip to main content

pytest plugin for URL based testing

Project description

pytest-base-url is a simple plugin for pytest that provides an optional base URL via the command line or configuration file.

License PyPI Travis Issues Requirements

Requirements

You will need the following prerequisites in order to use pytest-base-url:

  • Python 2.6, 2.7, 3.3, 3.4, 3.5, PyPy, or PyPy3

  • py.test 2.7 or newer

Installation

To install pytest-base-url:

$ pip install pytest-base-url

Specifying a Base URL

Rather than repeating or abstracting a base URL in your tests, pytest-base-url provides a base_url fixture that returns the specified base URL.

import urllib2

def test_example(base_url):
    assert 200 == urllib2.urlopen(base_url).getcode()

Using the Command Line

You can specify the base URL on the command line:

$ py.test --base-url http://www.example.com

Using a Configuration File

You can specify the base URL using a configuration file:

[pytest]
base_url = http://www.example.com

Using a Fixture

If your test harness takes care of launching an instance of your application under test, you may not have a predictable base URL to provide on the command line. Fortunately, it’s easy to override the base_url fixture and return the correct URL to your test.

In the following example a live_server fixture is used to start the application and live_server.url returns the base URL of the site.

import urllib2
import pytest

@pytest.fixture
def base_url(live_server):
    return live_server.url

def test_search(base_url):
    assert 200 == urllib2.urlopen('{0}/search'.format(base_url)).getcode()

Available Live Servers

It’s relatively simple to create your own live_server fixture, however you may be able to take advantage of one of the following:

  • Django applications can use pytest-django’s live_server fixture.

  • Flask applications can use pytest-flask’s 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.

Skipping Base URLs

You can skip tests based on the value of the base URL so long as it is provided either by the command line or in a configuration file:

import urllib2
import pytest

@pytest.mark.skipif(
    'dev' in pytest.config.getoption('base_url'),
    reason='Search not available on dev')
def test_search(base_url):
    assert 200 == urllib2.urlopen('{0}/search'.format(base_url)).getcode()

Unfortunately if the URL is provided by a fixture, there is no way to know this value at test collection.

Resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-base-url-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

pytest_base_url-1.0.0-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest-base-url-1.0.0.tar.gz.

File metadata

File hashes

Hashes for pytest-base-url-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5ffc137071d289dfb4c55403032ed81a83b7173495c58230f9a4cba59442e31c
MD5 1ed7a472a2819664aadedb092bb7b606
BLAKE2b-256 44a23bbeff1137c4791f11247e39d4ff1b7f54735d76eddc3f20f0cb93f145c6

See more details on using hashes here.

File details

Details for the file pytest_base_url-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_base_url-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e7955cb9222f63774d11579bfa5bc5739448adc954646696784c2d973390868e
MD5 c1cda0b2196e83e2af2ddde7955873ad
BLAKE2b-256 d64bbf3f02b82eba26a342b80e40afffc3eff44acefeab7161d6b563be262816

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page