Enable CamelCase-aware pytest class collection
Allow pytest to respect word boundaries of CamelCaseWords in class name patterns.
pip install pytest-camel-collect
This plug-in augments the pattern matching of
in your pytest.ini, tox.ini, or setup.cfg file.
- (dash) now represents a CamelCase word boundary.
[pytest] python_classes = Camel-*
Camel-* will match class names like
CamelCamelCamel, but not
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...
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
ForbidsNonAdmins mixins... but because
they don't inherit
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
just because you also want to run tests in your
[pytest] python_classes = Test-* Describe-* For-*
That's the spirit! Now,
TestStuff will be collected, but not
DescribeStuff will be collected, but not
DescribesCosas; and most importantly,
ForSpanishSpeakers will be collected, but not
To play around with the project and run its tests:
- Clone the repo
- In a virtualenv (or whatever you wanna do, I don't control you), run
pip install -e .[dev,test]
py.testto run the tests