Skip to main content

pytest-base-url

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 3.8+ or PyPy3

Installation

To install pytest-base-url:

$ pip install pytest-base-url

Contributing

We welcome contributions.

To learn more, see Development

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:

$ pytest --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 an Environment Variable

You can specify the base URL by setting the PYTEST_BASE_URL environment variable.

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 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

Metadata

Release files for pytest-base-url 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-base-url 2.1.0
File Size Uploaded
pytest_base_url-2.1.0.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-base-url 2.1.0
File Interpreter ABI Platform
pytest_base_url-2.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.0 kB

Release files / pytest_base_url-2.1.0.tar.gz

Download URL pytest_base_url-2.1.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
02748589a54f9e63fcbe62301d6b0496da0d10231b753e950c63e03aee745d45
BLAKE2b-256 checksum
How to use checksums
ae1ab64ac368de6b993135cb70ca4e5d958a5c268094a3a2a4cac6f0021b6c4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pytest_base_url-2.1.0-py3-none-any.whl

Download URL pytest_base_url-2.1.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ad15611778764d451927b2a53240c1a7a591b521ea44cebfe45849d2d2812e6
BLAKE2b-256 checksum
How to use checksums
981cb00940ab9eb8ede7897443b771987f2f4a76f06be02f1b3f01eb7567e24a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

2.1.0 This release

2 release files

2.0.0

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page