From the course: Unit Testing in Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Factory fixtures

Factory fixtures - Python Tutorial

From the course: Unit Testing in Python

Start my 1-month free trial

Factory fixtures

- [Instructor] So far, we've used fixtures to return hardcoded 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 test. Let's take a look at line 11, which has the process_data fixture, and test factorystart.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 hardcoding 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 hardcodes 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. At this point, we can rewrite the…

Contents