pytest-base-url is a pytest plugin that provides a `base_url` fixture for testing web applications. It allows you to define a base URL, from which derived `host` and `port` fixtures are automatically available in your tests. The current version is 2.1.0, and releases are generally tied to `pytest` compatibility or new features as needed, without a fixed cadence.
Warnings
breaking The `--base-url` command-line option was removed in version 2.0.0. This significantly changes how the base URL is configured.
Fix: Instead of `--base-url`, define the base URL in your `pytest.ini` file under the `[pytest]` section (e.g., `base_url = https://example.com/`) or directly within a custom `pytest_base_url` fixture in your `conftest.py`.
gotcha Forgetting to configure `base_url` or misconfiguring it (e.g., missing `http://` or `https://` schema, incorrect port, or trailing slash issues) can lead to unexpected test failures.
Fix: Always ensure your `base_url` in `pytest.ini` is a complete, valid URL including the scheme and a trailing slash if necessary for your application context (e.g., `https://api.example.com/`). Verify its value in tests using `print(base_url)` or assertions.
gotcha The `host` and `port` fixtures are derived directly from `base_url`. If your `base_url` is missing a port or has an invalid format, `host` and `port` might not be correctly extracted.
Fix: Ensure your `base_url` is consistently formatted. If a specific port is needed, include it explicitly in `base_url` (e.g., `https://example.com:8080/`). The `port` fixture will return `None` if no port is specified in the URL.
Install
pip install pytest-base-urlInstall plugin
Imports
base_url fixture
def test_my_endpoint(base_url, host, port):
pytest-base-url primarily provides fixtures ('base_url', 'host', 'port') that are automatically discovered and injected into your tests by the pytest framework. You do not import these fixtures directly using `from ... import ...` statements in your test files.
Quickstart
To quickly get started, first install the plugin. Then, configure your base URL in a `pytest.ini` file in your project's root directory under the `[pytest]` section. Finally, write your tests, making sure to declare `base_url`, `host`, or `port` as arguments in your test functions to utilize the injected fixtures.