All Projects → pytest-dev → Pytest Describe

pytest-dev / Pytest Describe

Licence: mit
Describe-style plugin for the pytest framework

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pytest Describe

Wemake Django Template
Bleeding edge django template focused on code quality and security.
Stars: ✭ 1,141 (+791.41%)
Mutual labels:  pytest
Pytest Regressions
Pytest plugin for regression testing: https://pytest-regressions.readthedocs.io
Stars: ✭ 89 (-30.47%)
Mutual labels:  pytest
Pytest Monitor
Pytest plugin for analyzing resource usage during test sessions
Stars: ✭ 105 (-17.97%)
Mutual labels:  pytest
Molecule Ansible Docker Aws
Example project showing how to test Ansible roles with Molecule using Testinfra and a multiscenario approach with Docker, Vagrant & AWS EC2 as infrastructure providers
Stars: ✭ 72 (-43.75%)
Mutual labels:  pytest
Education Backend
Django backend for my info-business website
Stars: ✭ 79 (-38.28%)
Mutual labels:  pytest
Frost
Unit testing framework for test driven security of AWS, GCP, Heroku and more.
Stars: ✭ 91 (-28.91%)
Mutual labels:  pytest
Integration tests
ManageIQ integration tests
Stars: ✭ 63 (-50.78%)
Mutual labels:  pytest
Python Blueprint
🐍 Example Python project using best practices 🔩
Stars: ✭ 123 (-3.91%)
Mutual labels:  pytest
Pytest Instafail
py.test plugin to show failures instantly
Stars: ✭ 86 (-32.81%)
Mutual labels:  pytest
Pytest Recording
A pytest plugin that allows recording network interactions via VCR.py
Stars: ✭ 98 (-23.44%)
Mutual labels:  pytest
Syrupy
🥞 The sweeter pytest snapshot plugin
Stars: ✭ 73 (-42.97%)
Mutual labels:  pytest
Pytest Json Report
🗒️ A pytest plugin to report test results as JSON
Stars: ✭ 77 (-39.84%)
Mutual labels:  pytest
Pytest Cookies
The pytest plugin for your Cookiecutter templates. 🍪
Stars: ✭ 96 (-25%)
Mutual labels:  pytest
Pytest Spec
Library pytest-spec is a pytest plugin to display test execution output like a SPECIFICATION.
Stars: ✭ 65 (-49.22%)
Mutual labels:  pytest
Wagtail Pipit
Pipit is a Wagtail CMS boilerplate which aims to provide an easy and modern developer workflow with a React-rendered frontend.
Stars: ✭ 109 (-14.84%)
Mutual labels:  pytest
Pytest Grpc
Allow test gRPC with pytest
Stars: ✭ 63 (-50.78%)
Mutual labels:  pytest
Pytest Deadfixtures
Plugin to list unused fixtures in your tests
Stars: ✭ 89 (-30.47%)
Mutual labels:  pytest
Python Pytest Cases
Separate test code from test cases in pytest.
Stars: ✭ 127 (-0.78%)
Mutual labels:  pytest
Kubetest
Kubernetes integration testing in Python via pytest
Stars: ✭ 122 (-4.69%)
Mutual labels:  pytest
Pytest Repeat
pytest plugin for repeating test execution
Stars: ✭ 99 (-22.66%)
Mutual labels:  pytest

.. image:: https://badge.fury.io/py/pytest-describe.svg :target: https://pypi.org/project/pytest-describe/ :alt: PyPI version

.. image:: https://travis-ci.org/pytest-dev/pytest-describe.svg?branch=master :target: https://travis-ci.org/pytest-dev/pytest-describe :alt: Travis CI

Describe-style plugin for pytest

pytest-describe is a plugin for pytest that allows tests to be written in arbitrary nested describe-blocks, similar to RSpec (Ruby) and Jasmine (JavaScript).

The main inspiration for this was a video <https://www.youtube.com/watch?v=JJle8L8FRy0>_ by Gary Bernhardt.

Installation

You guessed it::

pip install pytest-describe

Example

.. code-block:: python

def describe_list():

    @pytest.fixture
    def list():
        return []

    def describe_append():

        def adds_to_end_of_list(list):
            list.append('foo')
            list.append('bar')
            assert list == ['foo', 'bar']

    def describe_remove():

        @pytest.fixture
        def list():
            return ['foo', 'bar']

        def removes_item_from_list(list):
            list.remove('foo')
            assert list == ['bar']

Why bother?

I've found that quite often my tests have one "dimension" more than my production code. The production code is organized into packages, modules, classes (sometimes), and functions. I like to organize my tests in the same way, but tests also have different cases for each function. This tends to end up with a set of tests for each module (or class), where each test has to name both a function and a case. For instance:

.. code-block:: python

def test_my_function_with_default_arguments():
def test_my_function_with_some_other_arguments():
def test_my_function_throws_exception():
def test_my_function_handles_exception():
def test_some_other_function_returns_true():
def test_some_other_function_returns_false():

It's much nicer to do this:

.. code-block:: python

def describe_my_function():
    def with_default_arguments():
    def with_some_other_arguments():
    def it_throws_exception():
    def it_handles_exception():

def describe_some_other_function():
    def it_returns_true():
    def it_returns_false():

It has the additional advantage that you can have marks and fixtures that apply locally to each group of test function.

With pytest, it's possible to organize tests in a similar way with classes. However, I think classes are awkward. I don't think the convention of using camel-case names for classes fit very well when testing functions in different cases. In addition, every test function must take a "self" argument that is never used.

The pytest-describe plugin allows organizing your tests in the nicer way shown above using describe-blocks. The functions inside the describe-blocks need not follow any special naming convention, they are always executed as tests unless they start with an underscore. The functions used for describe-blocks must start with describe_, but you can configure this prefix with the setting describe_prefixes in the pytest configuration file.

Shared Behaviors

If you've used rspec's shared examples or test class inheritance, then you may be familiar with the benefit of having the same tests apply to multiple "subjects" or "suts" (system under test).

.. code-block:: python

from pytest import fixture
from pytest_describe import behaves_like

def a_duck():
    def it_quacks(sound):
        assert sound == "quack"

@behaves_like(a_duck)
def describe_something_that_quacks():
    @fixture
    def sound():
        return "quack"

    # the it_quacks test in this describe will pass

@behaves_like(a_duck)
def describe_something_that_barks():
    @fixture
    def sound():
        return "bark"

    # the it_quacks test in this describe will fail (as expected)

Fixtures defined in the block that includes the shared behavior take precedence over fixtures defined in the shared behavior. This rule only applies to fixtures, not to other functions (nested describe blocks and tests). Instead, they are all collected as separate tests.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].