Skip to content

naming model factory functions #225

Open
@drobert

Description

@drobert

Non-standard but supported use case in FactoryBoy is using a factory function for the 'model' definition, e.g.:

def _some_factory_func(*args, **kwargs):
  # do some cleanup/advanced logic around the args, e.g.
  if "foo" in kwargs:
    def kwargs["bar"] # contrived example
  return MyModel(*args, **kwargs)
  
@register
@register(_name="my_model") # has no effect
class MyModelFactory(Factory):
  class Meta:
    model: _some_factory_func # this line

  prop1 = ...
  prop2 = ...

elsewhere I have register(MyModelFactory) in a conftest.py file

What pytest sees as registered fixtures include:

  • _some_factory_func
  • _some_factory_func__prop1
  • _some_factory_func__prop2
  • my_model_factory

This does not seem to change even if I use @register(_name="my_model"); the name ultimately renames _some_factory_func.

Similarly, named_model does not succeed as it assumes the model is a type.

Request here is to support a factory function with a model name.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions