New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow override_config for pytest #338
Allow override_config for pytest #338
Conversation
Vladas thank you for your contribution. Why this is needed? |
There are a lot of django projects where i use pytest as a test runner. Django-constance forces me to use TestCase inheritance to make override work @camilonova please let me know if it's clear enough |
Got it. I'm not able to make a decision here. Maybe @jezdez knows best. |
…amoshaytisV/django-constance into feature/pytest_override_config
Thanks @camilonova for raising this with me, I agree that this is a non-trivial feature that we need to discuss first. In general I'm in favor of improving the use of override_config when used with Pytest. But I'm not sure if providing a decorator like override_config fits the best practices of the Pytest community. E.g. pytest-django didn't implement a custom overrride_settings decorator to override Django's settings but instead chose to implement a Pytest fixture that allows modifying of settings in the test session. Similarly I would encourage you to implement this feature as a |
@jezdez I agree that for pytest it's better to use fixtures or markers but what initially stopped me from implementing in that way is complexity to make it work preserving simplicity of decorator and ability to change config for any scope. |
I understand, and appreciate your openness to investigate this further. As always I try to apply the mantra "it's easy to add, but is it also easy to maintain?" to such discussion. Your input and contribution is valued and welcome nevertheless.
|
# Conflicts: # tox.ini
@jezdez @camilonova sorry for being inactive for a long time. I finalized refactoring and feature in general, please review or feel free to assign propper reviewers if you are not available. |
constance/test/pytest.py
Outdated
yield | ||
|
||
|
||
class ConstanceConfigWrapper(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably the need to have __setattr__
in this class may appear in future but ATM i can't think of many usage cases and all they can be solved using context manager or using fixture and marking tests/classes/modules
I like what's been done here, I'm using a copy paste on my code and it makes tests really clean. |
@jezdez will you be so kind to assign other reviewers if you are busy with smth else? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @TamoshaytisV 👍. I have a few suggestions - instead of pytest.mark
and ConstanceConfigWrapper
and override_config
fixture we could define override_config
as ContextDecorator
which could server both as decorator and context manager.
Co-Authored-By: Paweł Zarębski <ppjzarebski@gmail.com>
Please check the latest changes/comments and I would be happy to merge this. Thank you all! |
@pawelzar I used |
Thank you all |
Is this going to be released soon? |
If someone can confirm it works fine if the package is installed from master I can make a release |
I've tried it in my project with -e git+https://github.com/jazzband/django-constance.git#egg=constance[database] I've got the warning In fact, lib was really installed with database backend anyway, so in general, it's working |
""" | ||
Validate constance override marker params. Run test with overrided config. | ||
""" | ||
marker = item.get_closest_marker("override_config") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need something like here (in pytest-django) to prevent #410
marker = item.get_closest_marker("override_config") | |
if not django_settings_is_configured(): | |
return | |
marker = item.get_closest_marker("override_config") |
override_config
is currently possible to apply only to unittest TestCase classes.This PR adds
constance.test.pytest
module that can be used as followsExamples can be found in doc
It's still possible to do import as usual for unittest