pytest-base-url


Namepytest-base-url JSON
Version 2.1.0 PyPI version JSON
download
home_page
Summarypytest plugin for URL based testing
upload_time2024-01-31 22:43:00
maintainer
docs_urlNone
author
requires_python>=3.8
license
keywords automation base mozilla pytest url
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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.

.. image:: https://img.shields.io/badge/license-MPL%202.0-blue.svg
   :target: https://github.com/pytest-dev/pytest-base-url/blob/master/LICENSE
   :alt: License
.. image:: https://img.shields.io/pypi/v/pytest-base-url.svg
   :target: https://pypi.python.org/pypi/pytest-base-url/
   :alt: PyPI
.. image:: https://img.shields.io/travis/pytest-dev/pytest-base-url.svg
   :target: https://travis-ci.org/pytest-dev/pytest-base-url/
   :alt: Travis
.. image:: https://img.shields.io/github/issues-raw/pytest-dev/pytest-base-url.svg
   :target: https://github.com/pytest-dev/pytest-base-url/issues
   :alt: Issues
.. image:: https://img.shields.io/requires/github/pytest-dev/pytest-base-url.svg
   :target: https://requires.io/github/pytest-dev/pytest-base-url/requirements/?branch=master
   :alt: 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:

.. code-block:: bash

  $ pip install pytest-base-url

Contributing
------------

We welcome contributions.

To learn more, see `Development <https://github.com/pytest-dev/pytest-base-url/blob/master/development.rst>`_

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.

.. code-block:: python

  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:

.. code-block:: bash

  $ pytest --base-url http://www.example.com

Using a Configuration File
^^^^^^^^^^^^^^^^^^^^^^^^^^

You can specify the base URL using a `configuration file`_:

.. code-block:: ini

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

.. code-block:: python

  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:

.. code-block:: python

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

- `Release Notes`_
- `Issue Tracker`_
- Code_

.. _pytest: http://www.python.org/
.. _configuration file: http://pytest.org/latest/customize.html#command-line-options-and-configuration-file-settings
.. _pytest-django: http://pytest-django.readthedocs.org/
.. _pytest-flask: http://pytest-flask.readthedocs.org/
.. _skip tests: http://pytest.org/latest/skipping.html
.. _Release Notes:  http://github.com/pytest-dev/pytest-base-url/blob/master/CHANGES.rst
.. _Issue Tracker: http://github.com/pytest-dev/pytest-base-url/issues
.. _Code: http://github.com/pytest-dev/pytest-base-url

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pytest-base-url",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "automation,base,mozilla,pytest,url",
    "author": "",
    "author_email": "Dave Hunt <dhunt@mozilla.com>, Jim Brannlund <jimbrannlund@fastmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/ae/1a/b64ac368de6b993135cb70ca4e5d958a5c268094a3a2a4cac6f0021b6c4f/pytest_base_url-2.1.0.tar.gz",
    "platform": null,
    "description": "pytest-base-url\n===============\n\npytest-base-url is a simple plugin for pytest_ that provides an optional base\nURL via the command line or configuration file.\n\n.. image:: https://img.shields.io/badge/license-MPL%202.0-blue.svg\n   :target: https://github.com/pytest-dev/pytest-base-url/blob/master/LICENSE\n   :alt: License\n.. image:: https://img.shields.io/pypi/v/pytest-base-url.svg\n   :target: https://pypi.python.org/pypi/pytest-base-url/\n   :alt: PyPI\n.. image:: https://img.shields.io/travis/pytest-dev/pytest-base-url.svg\n   :target: https://travis-ci.org/pytest-dev/pytest-base-url/\n   :alt: Travis\n.. image:: https://img.shields.io/github/issues-raw/pytest-dev/pytest-base-url.svg\n   :target: https://github.com/pytest-dev/pytest-base-url/issues\n   :alt: Issues\n.. image:: https://img.shields.io/requires/github/pytest-dev/pytest-base-url.svg\n   :target: https://requires.io/github/pytest-dev/pytest-base-url/requirements/?branch=master\n   :alt: Requirements\n\nRequirements\n------------\n\nYou will need the following prerequisites in order to use pytest-base-url:\n\n- Python 3.8+ or PyPy3\n\nInstallation\n------------\n\nTo install pytest-base-url:\n\n.. code-block:: bash\n\n  $ pip install pytest-base-url\n\nContributing\n------------\n\nWe welcome contributions.\n\nTo learn more, see `Development <https://github.com/pytest-dev/pytest-base-url/blob/master/development.rst>`_\n\nSpecifying a Base URL\n---------------------\n\nRather than repeating or abstracting a base URL in your tests, pytest-base-url\nprovides a ``base_url`` fixture that returns the specified base URL.\n\n.. code-block:: python\n\n  import urllib2\n\n  def test_example(base_url):\n      assert 200 == urllib2.urlopen(base_url).getcode()\n\nUsing the Command Line\n^^^^^^^^^^^^^^^^^^^^^^\n\nYou can specify the base URL on the command line:\n\n.. code-block:: bash\n\n  $ pytest --base-url http://www.example.com\n\nUsing a Configuration File\n^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nYou can specify the base URL using a `configuration file`_:\n\n.. code-block:: ini\n\n  [pytest]\n  base_url = http://www.example.com\n\nUsing an Environment Variable\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nYou can specify the base URL by setting the ``PYTEST_BASE_URL`` environment variable.\n\nUsing a Fixture\n^^^^^^^^^^^^^^^\n\nIf your test harness takes care of launching an instance of your application\nunder test, you may not have a predictable base URL to provide on the command\nline. Fortunately, it's easy to override the ``base_url`` fixture and return\nthe correct URL to your test.\n\nIn the following example a ``live_server`` fixture is used to start the\napplication and ``live_server.url`` returns the base URL of the site.\n\n.. code-block:: python\n\n  import urllib2\n  import pytest\n\n  @pytest.fixture\n  def base_url(live_server):\n      return live_server.url\n\n  def test_search(base_url):\n      assert 200 == urllib2.urlopen('{0}/search'.format(base_url)).getcode()\n\nAvailable Live Servers\n----------------------\n\nIt's relatively simple to create your own ``live_server`` fixture, however you\nmay be able to take advantage of one of the following:\n\n* Django applications can use pytest-django_'s  ``live_server`` fixture.\n* Flask applications can use pytest-flask_'s ``live_server`` fixture.\n\nVerifying the Base URL\n----------------------\n\nIf you specify a base URL for a site that's unavailable then all tests using\nthat base URL will likely fail. To avoid running every test in this instance,\nyou can enable base URL verification. This will check the base URL is\nresponding before proceeding with the test suite. To enable this, specify the\n``--verify-base-url`` command line option or set the ``VERIFY_BASE_URL``\nenvironment variable to ``TRUE``.\n\nSkipping Base URLs\n------------------\n\nYou can `skip tests`_ based on the value of the base URL so long as it is\nprovided either by the command line or in a configuration file:\n\n.. code-block:: python\n\n  import urllib2\n  import pytest\n\n  @pytest.mark.skipif(\n      \"'dev' in config.getoption('base_url')\",\n      reason='Search not available on dev')\n  def test_search(base_url):\n      assert 200 == urllib2.urlopen('{0}/search'.format(base_url)).getcode()\n\nUnfortunately if the URL is provided by a fixture, there is no way to know this\nvalue at test collection.\n\nResources\n---------\n\n- `Release Notes`_\n- `Issue Tracker`_\n- Code_\n\n.. _pytest: http://www.python.org/\n.. _configuration file: http://pytest.org/latest/customize.html#command-line-options-and-configuration-file-settings\n.. _pytest-django: http://pytest-django.readthedocs.org/\n.. _pytest-flask: http://pytest-flask.readthedocs.org/\n.. _skip tests: http://pytest.org/latest/skipping.html\n.. _Release Notes:  http://github.com/pytest-dev/pytest-base-url/blob/master/CHANGES.rst\n.. _Issue Tracker: http://github.com/pytest-dev/pytest-base-url/issues\n.. _Code: http://github.com/pytest-dev/pytest-base-url\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "pytest plugin for URL based testing",
    "version": "2.1.0",
    "project_urls": {
        "Homepage": "https://github.com/pytest-dev/pytest-base-url",
        "Source": "https://github.com/pytest-dev/pytest-base-url",
        "Tracker": "https://github.com/pytest-dev/pytest-base-url/issues"
    },
    "split_keywords": [
        "automation",
        "base",
        "mozilla",
        "pytest",
        "url"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "981cb00940ab9eb8ede7897443b771987f2f4a76f06be02f1b3f01eb7567e24a",
                "md5": "dcd349af3436bed252e97090039fc137",
                "sha256": "3ad15611778764d451927b2a53240c1a7a591b521ea44cebfe45849d2d2812e6"
            },
            "downloads": -1,
            "filename": "pytest_base_url-2.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dcd349af3436bed252e97090039fc137",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5302,
            "upload_time": "2024-01-31T22:42:58",
            "upload_time_iso_8601": "2024-01-31T22:42:58.897844Z",
            "url": "https://files.pythonhosted.org/packages/98/1c/b00940ab9eb8ede7897443b771987f2f4a76f06be02f1b3f01eb7567e24a/pytest_base_url-2.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ae1ab64ac368de6b993135cb70ca4e5d958a5c268094a3a2a4cac6f0021b6c4f",
                "md5": "997406179aae7d7991abb498d25dbf5f",
                "sha256": "02748589a54f9e63fcbe62301d6b0496da0d10231b753e950c63e03aee745d45"
            },
            "downloads": -1,
            "filename": "pytest_base_url-2.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "997406179aae7d7991abb498d25dbf5f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6702,
            "upload_time": "2024-01-31T22:43:00",
            "upload_time_iso_8601": "2024-01-31T22:43:00.810547Z",
            "url": "https://files.pythonhosted.org/packages/ae/1a/b64ac368de6b993135cb70ca4e5d958a5c268094a3a2a4cac6f0021b6c4f/pytest_base_url-2.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-31 22:43:00",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pytest-dev",
    "github_project": "pytest-base-url",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pytest-base-url"
}
        
Elapsed time: 2.07222s