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

Start to use factories to generate test data #554

Merged
merged 8 commits into from
Mar 8, 2023
Merged

Conversation

grahamalama
Copy link
Contributor

@grahamalama grahamalama commented Feb 17, 2023

We use static data as test data throughout our tests. Some observations I've made about this data and the tests that use them:

  • it makes test setup not obvious. I ask myself, "when a given test starts, what data is already present in the database?"
  • they promote the use of "magic numbers"
  • they're used in complex fixtures
  • The division between "minimal" and "maximal" contact seems arbitrary

In this PR, we would introduce the use of test factories with Factory Boy and pytest-factoryboy. I've done minimal setup in this PR to demonstrate a proof of concept. This involved adding a scoped_session for database tests as well as making two example factories and using them in two example tests.

If we like this pattern, we can use it going forward with the goal of making existing tests easier to understand and new tests easier to write.

@grahamalama grahamalama requested a review from a team as a code owner February 17, 2023 17:20
@grahamalama grahamalama added the enhancement New feature or request label Feb 17, 2023
@grahamalama grahamalama changed the title [WIP] Proposal: Use factories to generate test data Proposal: Use factories to generate test data Feb 23, 2023
Copy link
Contributor

@leplatrem leplatrem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!

Since migrating the static data to this will require some efforts, what are the next steps? Merge this and live with the 2 ways, and migrate later?

@grahamalama
Copy link
Contributor Author

Merge this and live with the 2 ways, and migrate later?

Potentially. I don't know if this is a viable solution, but we could make it a point in PR review to say, "Did you modify a test"? If so, replace any static fixture data with a factory."

tests/unit/conftest.py Outdated Show resolved Hide resolved
@grahamalama grahamalama self-assigned this Mar 6, 2023
Comment on lines +39 to +40
double_opt_in = False
has_opted_out_of_email = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to modify this for active/inactive contacts? (I think active contacts should have double_opt_in to True)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we will have to override this default for different test cases -- either we keep this as is and override double_opt_in where applicable, or vice versa.

We could also create additional factories with different defaults for different test cases.

@grahamalama grahamalama force-pushed the test-data-factories branch from b561dfb to b331d5b Compare March 8, 2023 18:41
@grahamalama grahamalama changed the title Proposal: Use factories to generate test data Start to use factories to generate test data Mar 8, 2023
@grahamalama grahamalama merged commit eceaf7f into main Mar 8, 2023
@grahamalama grahamalama deleted the test-data-factories branch March 8, 2023 18:50
@grahamalama grahamalama linked an issue Apr 14, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace static fixtures with factory-generated fixtures
3 participants