Skip to main content

Relaxed test discovery/organization for pytest

Project description

pytest-relaxed provides ‘relaxed’ test discovery for pytest.

It is the spiritual successor to https://pypi.python.org/pypi/spec, but is built for pytest instead of nosetests, and rethinks some aspects of the design (such as a decreased emphasis on the display side of things.)

Rationale

Has it ever felt strange to you that we put our tests in tests/, then name the files test_foo.py, name the test classes TestFoo, and finally name the test methods test_foo_bar? Especially when almost all of the code inside of tests/ is, well, tests?

This pytest plugin takes a page from the rest of Python, where you don’t have to explicitly note public module/class members, but only need to hint as to which ones are private. By default, all files and objects pytest is told to scan will be considered tests; to mark something as not-a-test, simply prefix it with an underscore.

Relaxed discovery

The “it’s a test by default unless underscored” approach works for files:

tests
    ├── _util.py
    ├── one_module.py
    └── another_module.py

It’s applied to module members:

def _helper():
    pass

def one_thing():
    assert True

def another_thing():
    assert False

def yet_another():
    assert _helper() == 'something'

And to class members:

class SomeObject(object):
    def behavior_one(self):
        assert True

    def another_behavior(self):
        assert False

    def _helper(self):
        pass

    def it_does_things(self):
        assert self._helper() == 'whatever'

Exceptions

As you might expect, there are a few more special cases around discovery to avoid fouling up common test extensions:

  • Files named conftest.py aren’t treated as tests, because they do special pytest things;

  • Module and class members named setup_(module|class|method|function) are not considered tests, as they are how pytest implements classic/xunit style setup and teardown;

  • Objects decorated as fixtures with @pytest.fixture are, of course, also skipped.

Nested class organization

On top of the relaxed discovery algorithm, pytest-relaxed also lets you organize tests in a nested fashion, again like the spec nose plugin or the tools that inspired it, such as Ruby’s rspec.

This is purely optional, but we find it’s a nice middle ground between having a proliferation of files or suffering a large, flat test namespace making it hard to see which feature areas have been impacted by a bug (or whatnot).

The feature is enabled by using nested/inner classes, like so:

class Some(object):
    def basic_behavior(self):
        assert True

    class init:
        "__init__"

        def no_args_required(self):
            assert True

        def accepts_some_arg(self):
            assert True

        def sets_up_config(self):
            assert False

    class some_method:
        def accepts_whatever_params(self):
            assert False

        def base_behavior(self):
            assert True

        class when_config_says_foo:
            def it_behaves_like_this(self):
                assert False

        class when_config_says_bar:
            def it_behaves_like_this(self):
                assert True

Test discovery on these inner classes is recursive, so you can nest them as deeply as you like. Naturally, as with all Python code, sometimes you can have too much of a good thing…but that’s up to you.

Installation & use

As with most pytest plugins, it’s dead simple:

  • pip install pytest-relaxed;

  • Tell pytest where your tests live; otherwise pytest-relaxed will cause pytest to load all of your code as tests! We recommend using setup.cfg or similar:

    [tool:pytest]
    testpaths = tests
  • Write some tests, as exampled above;

  • pytest to run the tests, and you’re done!

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-relaxed-0.0.3.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_relaxed-0.0.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest-relaxed-0.0.3.tar.gz.

File metadata

File hashes

Hashes for pytest-relaxed-0.0.3.tar.gz
Algorithm Hash digest
SHA256 47155750c937aa555c410b2ae1316da1b51da703fc923e14915c7ca59923c4f8
MD5 6aeab9993f717ac9c4169b42865568e2
BLAKE2b-256 60f3301f5f5711e58aac46fe7042ca0037ccc02e2e0a53175385563f3c7a6579

See more details on using hashes here.

File details

Details for the file pytest_relaxed-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_relaxed-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2908b5647d9a672b55648f513e7b7f45245efadc7228f7339b1506f4425ec7c0
MD5 b8246a24569e080c20e982e92b84242b
BLAKE2b-256 c0eaa6d228b269642c3c2854c770ec3396b480b961bde19d3fe1cdf48060fc42

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page