From the course: Unit Testing in Python

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Factory fixtures

Factory fixtures

- [Instructor] So far, we've used fixtures to return hard coded objects directly. We can instead customize our fixtures to return an object based on what arguments are passed to it. This allows us to make use of the fixture as a factory, generating an object based on the needs of our tests. Let's take a look at line 11, which has the process_data fixture in test_factory_start.py. In this version of the test, our fixture is set up to process data for the clean_map.csv only. This is because it uses the city_list_location fixture on line six. That fixture is hard coding the location of the file. If we wanted to process another CSV file or even JSON data, we would need to duplicate code. You can see that on line 17 where we've created a separate fixture that hard codes the path to the malformed CSV, which will be used in our Sad Path test. This is not ideal to continue testing in this fashion. As an update, a more intuitive way to set up this function would be to reverse the logic shown…

Contents