I am using Faker from inside factory boy and we are getting duplicate values that are making our tests fail. Faker has the ability to generate unique values, but it has a finite number of values for a given provider like first_name. We have 100s of tests and after a while all the unique values have been used and we get a UniquenessException. I read there is a unique.clear() method which clears the already seen values, however, this seems not to work. I instrumented faker/proxy.py to log when clear is called and what the length of the already seen values is and although clear is called the length continues to increase. I am thinking it's an issue with scope, but I have tried it many different ways. Faker is called from our factories. I have tried calling it from an autouse function run before each test, and even at the start of each test. I need to instantiate Faker to call the clear function - perhaps each time I do that it is not the same instance the factories use.
Further instrumenting the faker code shows that when faker is called to get some data it's one instance (the same one every time), but when clear is called it's a different instance. Need to get a handle for the instance the data is being generated from.
How can I do that?
I have solved this. I added this to conftest:
Now clear is getting called before each test in the same instance of faker as the one we use to get data from, the already seen cache is getting cleared out and I am not seeing any duplicate key issues.