Skip to main content

Enable CamelCase-aware pytest class collection

Project description

pytest-camel-collect

PyPI version

Allow pytest to respect word boundaries of CamelCaseWords in class name patterns.

Installation

pip install pytest-camel-collect

Usage

This plug-in augments the pattern matching of python_classes in your pytest.ini, tox.ini, or setup.cfg file.

A - (dash) now represents a CamelCase word boundary.

[pytest]
python_classes = Camel-*

Camel-* will match class names like CamelClub and CamelCamelCamel, but not Camelizer.

Why?

Mixin classes can be helpful to reduce boilerplate. One might use these mixin classes to add tests verifying API response status codes when authenticated as different users:

class ForbidsAnonymousUsers:
    class TestAnonymousUsersAreForbidden:
        @pytest.fixture
        def user(self):
            return AnonymousUser()

        def test_anonymous_user_is_forbidden(self, response):
            assert response.status_code == 401

class ForbidsNonAdmins:
    class TestNonAdminsAreForbidden:
        @pytest.fixture
        def user(self):
            return User(is_admin=False)

        def test_non_admin_is_forbidden(self, response):
            assert response.status_code == 401

Now, these mixins can be used to declare "traits" of certain test environments:

class DescribeMyAPIEndpoint(BaseAPITest):
    @pytest.fixture
    def url(self):
        return '/my-endpoint'

    class DescribeList(
        ForbidsAnonymousUsers,
    ):
        @pytest.fixture
        def method(self):
            return 'GET'

    class DescribeCreate(
        ForbidsAnonymousUsers,
        ForbidsNonAdmins,
    ):
        @pytest.fixture
        def method(self):
            return 'POST'

As it goes, business requirements change, and the API endpoint must now respond differently depending on the user's language.

No sweat! As experts in nameology, we add well-named context classes to test other languages:

class DescribeMyAPIEndpoint(BaseAPITest):
    # ...

    class DescribeCreate(
        ForbidsAnonymousUsers,
        ForbidsNonAdmins,
    ):
        # ...

        class ForEnglishSpeakers:
            @pytest.fixture
            def user(self, user):
                user.language = 'english'
                return user

            def it_returns_english(self, response):
                assert response['message'] == 'Created new thing'

        class ForSpanishSpeakers:
            @pytest.fixture
            def user(self, user):
                user.language = 'spanish'
                return user

            def it_returns_spanish(self, response):
                assert response['message'] == 'Creado cosa nueva'

Hmmm, but when pytest is executed, it doesn't collect our two new tests...

Ah, right! python_classes in pytest.ini!

[pytest]
python_classes = Test* Describe* For*

Run pytest again and it picks up our tests! Oh, and also picks up our ForbidsAnonymousUsers and ForbidsNonAdmins mixins... but because they don't inherit BaseAPITest, the response fixture doesn't exist, and they fail.

What ever will we do?

Introducing: pytest-camel-collect, the pytest plugin enabling you, the hard-working, dependable, definitely-not-sleep-deprived developer to explicitly match CamelCase words during pytest collection.

No longer must you run tests from your ForbidsAnonymousUsers mixin, just because you also want to run tests in your ForSpanishSpeakers context! Hell no!

[pytest]
python_classes = Test-* Describe-* For-*

That's the spirit! Now, TestStuff will be collected, but not Testimony; DescribeStuff will be collected, but not DescribesCosas; and most importantly, ForSpanishSpeakers will be collected, but not ForbidsAnonymousUsers.

Development

To play around with the project and run its tests:

  1. Clone the repo
  2. In a virtualenv (or whatever you wanna do, I don't control you), run pip install -e .[dev,test]
  3. Run py.test to run the tests

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-camel-collect-1.0.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

pytest_camel_collect-1.0.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pytest-camel-collect-1.0.2.tar.gz.

File metadata

  • Download URL: pytest-camel-collect-1.0.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2

File hashes

Hashes for pytest-camel-collect-1.0.2.tar.gz
Algorithm Hash digest
SHA256 de4d2a54db3f5dd743ab90c350f9481b19cd713684e4feae893121c728b0f09c
MD5 49d90ca918d5d2d8bdd8ab31cb4d810d
BLAKE2b-256 dcacf7bf8eb7b41bf73a235c885c05bba0cd211c27318566690896adc1ed89dd

See more details on using hashes here.

File details

Details for the file pytest_camel_collect-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pytest_camel_collect-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2

File hashes

Hashes for pytest_camel_collect-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d119d4908616d4999bc7b440d7aa71057d89334646ffc17f656c8977c619e945
MD5 284058e7cbc914beb25701d2d191d2b1
BLAKE2b-256 ed2021a8093330ab5c4bb421d647bc8dc698031a38f7404d05a4c6efa789919d

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