pytest-dev / pytest-dev/pytest-django
Not able to reset django form field initial value
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 367
- PR merge metrics
- No merged PRs in 30d
Description
I have a django form with a field defined like this:
myField = forms.CharField(
widget=forms.HiddenInput(),
initial=f"2023-{settings.FIELD_BASE}",
)
I'm writing a test where I'd like to use @pytest.mark.parametrize to test different values of FIELD_BASE. Something like:
@pytest.mark.parametrize("test_base", ["aa", "bb", "cc"])
def test_bases(self, client, settings, test_base):
settings.FIELD_BASE = test_base
response = client.get(reverse(someview))
assert f'name="myField" value="2023-{test_base}" in response.content
This does not work. My hunch is that it's because initial is set when the module is loaded at the beginning of the test run. So I tried reloading, adding the two lines after the settings update:
settings.FIELD_BASE = test_base
import importlib
importlib.reload('myapp.forms')
Still no luck.
Any thoughts on how to handle this?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the form definition in myapp.forms and the parametrized test_bases case shown in the issue; reproduce the behavior after changing settings.FIELD_BASE. Done means identifying and documenting a supported way to test the field's initial value, or establishing a minimal project change if the behavior is a pytest-django bug.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, python
- Domain
- backend, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100