polyfactory icon indicating copy to clipboard operation
polyfactory copied to clipboard

Auto-register factories as pytest fixtures

Open mikelane opened this issue 3 years ago • 1 comments

It would be super handy to have some method that would easily create a fixture out of a factory. For example:

class SomeLongNamedModel(BaseModel):
    id: UUID


@register_fixture   # Or some better name?
class SomeLongNamedModelFactory(ModelFactory):
    """Factory to create SomeLongNameModel instances
    
    Use this factory to generate SomeLongNameModels filled with random data
    """
    __model__ = SomeLongNamedModel

And then

$ pytest --fixtures

# other output...
some_long_named_model_factory
    Factory to create SomeLongNameModel instances
    
    Use this factory to generate SomeLongNameModels filled with random data

This is following the model of pytest-factoryboy which automatically converts the PascalCase class names to be the snake_case equivalent for the fixtures.

mikelane avatar Sep 09 '22 19:09 mikelane

Sure, this is a good suggestion. If you'd like to add this it would be a welcome addition.

Goldziher avatar Sep 09 '22 19:09 Goldziher

I think I could try to implenment this recently.

EltonChou avatar Oct 01 '22 08:10 EltonChou

Cool

Goldziher avatar Oct 01 '22 08:10 Goldziher