Skip to main content

A simple plugin to use with pytest

Project description

PyPI version Python versions See Build Status on Travis CI See Build Status on AppVeyor

pytest plugin providing a BDD ‘language’ for web app integration-testing.


Features

Implements a simple headless browser that requests web pages according to a URL mapping, parses web forms, and enables assertions about the various web responses using PyQuery selectors (similar to jQuery).

Example:

Feature: Homepage functionality
    Scenario: Homepage
        When the user requests list home
        Then response 0 status code is "200"
        And response 0 element "#main_title" contains 'Welcome'

    Scenario: User profile
        When the user requests view user_profile
        Then response 0 status code is "200"
        And response 0 element "tr:contains('Balance')" contains '45.67'
        And response 0 form-contains-array-outline <arrayfield> <arrayvalue>

        Examples:
        | arrayfield | arrayvalue |
        | choices    | [0 2 3]    |

Requires you to provide these fixtures:

  • client

  • url_mapping

client must implement get and post methods, compatible with Flask.test_client.

Example:

@pytest.fixture
def app():
    """Create, configure a new app instance for each test."""
    "..."
    return app

@pytest.fixture
def client(app):
    return app.test_client()

url_mapping must be a dictionary, with keys of top-level “groups”, and values mapping a human-readable “action” to an absolute URL in your web app.

Example:

URL_MAPPING = {
    'home': {
        'list': '/',
        'articles': '/latest/',
    },
    'user_profile': {
        'view': '/user/profile/',
        'update': '/user/profile/update',
    },
    'blog': {
        'delete': '/blog/{}/delete', # {} gets filled from feature args
    },
};
@pytest.fixture
def url_mapping():
    return URL_MAPPING

Available When Steps

  • the user requests {action} {what} [args…]

Will operate on the URL looked up from the url_mapping, with any args substituted in for occurrences of {} in the mapped URL.

Example:

When the user requests view user_profile
  • the user sets-array {afield} [{avalue}]

Sets a form value on the current web page.

Example:

When the user sets-array choices [1 3 4]
  • the user sets-array-outline <arrayfield> <arrayvalue>

Sets a form value on the current web page.

Example:

When the user sets-array-outline <arrayfield> <arrayvalue>

Examples:
| arrayfield | arrayvalue |
| choices    | [0 2 3]    |
  • the user sets {field} “{value}”

Sets a form value on the current web page.

Example:

When the user sets fullname "Big Bob"
  • the user submits requests {action} {what} [args…]

Will operate on the URL looked up from the url_mapping, with any args substituted in for occurrences of {} in the mapped URL.

Example:

When the user submits delete blog 4ec2d70

Available Then Steps

  • response {response_index:d} status code is “{code:d}”

Example:

Then response 0 status code is "200"
  • response {response_index:d} shows element “{selector}”

Example:

Then response 0 shows element "div.success"
  • response {response_index:d} form-contains-array-outline <arrayfield> <arrayvalue>

Then-clause asserting about a given outline form-field having the given outline array value.

Example:

Then response 0 form-contains-array-outline <arrayfield> <arrayvalue>

Examples:
| arrayfield | arrayvalue |
| choices    | [0 2 3]    |
  • response {response_index:d} json-has “{key}”

Example:

Then response 0 json-has "account_balance"
  • response {response_index:d} json-contains “{key}” “{phrase}”

Example:

Then response 0 json-contains "account_balance" "45.67"
  • response {response_index:d} doesn’t show element “{selector}”

Example:

Then response 0 doesn't show element "div.alert"
  • response {response_index:d} element “{selector}” doesn’t contain ‘{phrase}’

Example:

Then response 0 element "#username" doesn't contain 'bob'
  • response {response_index:d} element “{selector}” doesn’t contain-outline <contains>

Then-clause asserting about a PyQuery-selected element not containing an outline value.

Example:

Then response 0 element "#username" doesn't contain 'bob'
  • response {response_index:d} element “{selector}” doesn’t contain-outline <contains>

Then-clause asserting about a PyQuery-selected element not containing an outline value.

Example:

Then response 0 element "#response" doesn't contain-outline <contains>

Examples:
| contains |
| 0 items added |
  • response {response_index:d} element “{selector}” contains-outline <contains>

Then-clause asserting about a PyQuery-selected element containing an outline value.

Example:

Then response 0 element "#username" contains-outline <contains>

Examples:
| contains |
| Bob |

Requirements

PyQuery, pytest-bdd, mechanize

Installation

You can install “pytest-bdd-web” via pip from PyPI:

$ pip install pytest-bdd-web

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, “pytest-bdd-web” is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

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-bdd-web-0.1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

pytest_bdd_web-0.1.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest-bdd-web-0.1.1.tar.gz.

File metadata

  • Download URL: pytest-bdd-web-0.1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.9

File hashes

Hashes for pytest-bdd-web-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3f53fdbea2fb494869f943e26e75a685571cf2cd94fd2f40c267b9c4e8dff3f3
MD5 194fb337c89c477714ed2992e598bcb4
BLAKE2b-256 3e6a0624dc545cd165a376f5cad93c0a06bb22ecbb5b049cb2dcaeda8983ca8d

See more details on using hashes here.

File details

Details for the file pytest_bdd_web-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pytest_bdd_web-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.6.9

File hashes

Hashes for pytest_bdd_web-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e67a7477f5f52ee28a140ce3e3ef3244a6bf6f29777bd0ede7516d8a5acaa161
MD5 8e338e8f7201e07c0572906c06ade384
BLAKE2b-256 eb2e2902c1ade952bbbd0df99caa42ff70742c083234326592e31776760b1691

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