Best practices for tests that involve databases #5276
-
I have a very complex function with some defined behavior that I want to use property-based testing to ensure is correct (or find issues with). However, this function makes several database calls. Up to now I've been using a simple function with What's the recommended way to write a property-based test that deals with a database? Is it to reset the database between each property run? That seems like it would consume a lot of time... Additionally - how would you create an arbitrary for values that depend on database returns (i.e. a user needs a department ID, which requires saving the department to the database...)? I would really appreciate some help understanding what other people have done. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
#5022 is a similar question on how to randomize database relations. I think this also addresses this question if I understand you correctly:
Regarding this question:
There is a strong assumption that every test run starts on a clean slate and that no relevant state is carried over. Otherwise shrinking can give bogus results. For example, let's say your test fails (for whatever reason) when there is an email address in the database that contains a "+". One test run inserts Maybe it's fine to reset the database before every run. If it's too slow or cumbersome, is mocking away the database calls is an option? |
Beta Was this translation helpful? Give feedback.
#5022 is a similar question on how to randomize database relations. I think this also addresses this question if I understand you correctly:
Regarding this question:
There is a strong assumption that every test run starts on a clean slate and that no relevant state is carried over. Otherwise shrinking can give bogu…