-
Notifications
You must be signed in to change notification settings - Fork 766
Make tests order independent #932
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
Conversation
@ganwell if you need to drop Python 2.7 support then why not create this PR against the v3 branch? |
@jkimbo I rebased to v3 and removed WIP |
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.
Looking good @ganwell . Left some comments
graphene_django/conftest.py
Outdated
settings = dict(graphene_settings.__dict__) | ||
yield None | ||
reset_global_registry() | ||
graphene_settings.__dict__ = settings |
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.
Why do you have to restore the graphene_settings dict?
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.
There are tests that change the settings and don't change it back.
There are several options:
-
Provide a graphene_settings fixture (probably the best way). This fixture resets the settings after use
-
Just reset the settings on every test, so it is impossible to leak any settings. I decided to use this approach because it is best for an environment where contributors often change.
-
Manually reset the settings in the tests
We usually use 1. but we have regular contributors. Let me know what you prefer.
Uses of graphene_settings:
$> git grep graphene_settings | grep '/tests/'
graphene_django/forms/tests/test_mutation.py:from ...settings import graphene_settings
graphene_django/forms/tests/test_mutation.py: graphene_settings.CAMELCASE_ERRORS = True
graphene_django/forms/tests/test_mutation.py: graphene_settings.CAMELCASE_ERRORS = False
graphene_django/rest_framework/tests/test_mutation.py:from ...settings import graphene_settings
graphene_django/rest_framework/tests/test_mutation.py: graphene_settings.CAMELCASE_ERRORS = True
graphene_django/rest_framework/tests/test_mutation.py: graphene_settings.CAMELCASE_ERRORS = False
graphene_django/tests/test_converter.py:from ..settings import graphene_settings
graphene_django/tests/test_converter.py: graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = True
graphene_django/tests/test_converter.py: graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = False
graphene_django/tests/test_query.py:from ..settings import graphene_settings
graphene_django/tests/test_query.py: graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = True
graphene_django/tests/test_query.py: graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 100
graphene_django/tests/test_query.py: graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = False
graphene_django/tests/test_query.py: graphene_settings.RELAY_CONNECTION_MAX_LIMIT = 100
graphene_django/tests/test_query.py: graphene_settings.RELAY_CONNECTION_ENFORCE_FIRST_OR_LAST = False
graphene_django/tests/test_types.py:from ..settings import graphene_settings
graphene_django/tests/test_types.py: graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = True
graphene_django/tests/test_types.py: graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = False
graphene_django/tests/test_types.py: graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = (
graphene_django/tests/test_types.py: graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = None
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.
I think a fixture would be good as well. If it's going to be too much work to change it all though I'm ok leaving it like this.
text = forms.CharField() | ||
@pytest.fixture() | ||
def my_form(): | ||
class MyForm(forms.Form): |
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.
Why do the forms need to be fixtures?
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.
@jkimbo I think only the types have to be fixtures, because they get registered in the global registry through some magic when python executes the class. I put everything into fixtures just for consistency.
If you like I can check what needs to be fixtures and only more these to fixtures.
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.
I think that would be cleaner. As far as I'm aware only the Django models and Graphene-Django DjangoObjectType's register themselves into a registry.
assert result.errors[0].messages == ["This field is required."] | ||
assert result.errors[1].messages == ["This field is required."] | ||
assert "age" in fields_w_error | ||
assert "name" in fields_w_error |
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.
Much prefer plain pytest tests rather than Django's TestCase 👍
pytest.ini
Outdated
@@ -1,2 +1,3 @@ | |||
[pytest] | |||
DJANGO_SETTINGS_MODULE = django_test_settings | |||
addopts = --random-order |
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.
Pytest config shouldn't be defined in multiple places. I would remove the pytest.ini
file and just define it in the setup.cfg
file.
@jkimbo I think I addressed all the issues. |
@jkimbo I will split this PR into one that can be merge to master and the rest that can only be merged to v3. In about 2 hours. |
If you feel it's important @ganwell . I'm fine with just merging this into v3 |
@jkimbo I moved this PR to master again. It now contains everything except adding pytest-random-order and can therefore be merged to master. |
@jkimbo Here is the part that only works on v3 11e1403 / #940 Sorry for making it so complicated I felt it is the right thing to do in case it takes longer to merge v3 to master. I will checkin on #932 and #940 the next days. In case you think it is all too complicated just do what you think is best: ie commiting it yourself. |
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.
This is looking good thanks, @ganwell . Just one thing before I think we should merge this.
graphene_django/conftest.py
Outdated
|
||
|
||
@pytest.fixture() | ||
def settings(): |
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.
def settings(): | |
def graphene_settings(): |
Otherwise this will clash with the pytest django settings fixture: https://pytest-django.readthedocs.io/en/latest/helpers.html#settings
* Reset the global registry after each test (teardown) * Create a settings fixtures that returns graphene_settings and resets the graphene_settings after use (teardown) * Convert test_mutation tests from unittests.TestCase to pytest * Convert test_mutation PetType to a pet_type fixtures that reregisters the type
@ganwell v3 updated |
Reset the global registry after each test (teardown)
Create a settings fixtures that returns graphene_settings and resets the graphene_settings after use (teardown)
Convert test_mutation tests from unittests.TestCase to pytest
Convert test_mutation PetType to a pet_type fixtures that reregisters the type
The core change is setup/teardown in conftest.py
graphene-django/graphene_django/conftest.py
Lines 8 to 18 in a16cebe