Skip to content
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

Parametrize test_workspacecreateform_success #4654

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

iaindillingham
Copy link
Member

In this PR, we make a couple of small changes to two tests. These small changes help us see that the two tests are basically the same, save for two parameters. We then parameterise one test and remove the other, meaning we have the same tests for 14 fewer lines of code.

We use the pytest.mark.parametrize decorator to parameterise the test, also using pytest.param to assign a more descriptive ID to each parameter set. Doing so also makes it easier to run the test with either parameter set:

pytest tests/unit/jobserver/test_forms.py::test_workspacecreateform_success[lower_case_name]

pytest tests/unit/jobserver/test_forms.py::test_workspacecreateform_success[mixed_case_name]

Or with both parameter sets:

pytest tests/unit/jobserver/test_forms.py::test_workspacecreateform_success

Although out of scope, I think it's worth noting that test_workspacecreateform_success would be a great candidate for property-based testing. It's unlikely that every name will be either lower-case or mixed-case ASCII in real life, so verifying that WorkspaceCreateForm.clean_name works correctly with a wide range of names is important.

I created this PR whilst working on #4593. It doesn't address that issue, but it does make that issue easier to address.

This renames a test to better reflect its intent.
This adds an assertion to `test_workspacecreateform_success` that
matches the assertion in
`test_workspacecreateform_success_with_mixed_case_name`. In both cases,
the assertions check that the `name` field is cleaned.

It wasn't clear to me why the assertion message was the cleaned `name`
field (possibly for debugging?), so I removed the assertion message.
If you place them side-by-side, then you should be able to see that
`test_workspacecreateform_success` and
`test_workspacecreateform_success_with_mixed_case_name` are identical,
save for the value of `name`. Let's parametrize the former and remove
the latter, using `pytest.param` to highlight what's important about
each parameter set.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants