pytest-dev / pytest-dev/pytest-factoryboy

naming model factory functions

Open
#225 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
400
Forks
44
Avg merge
6h 34m
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how register derives names for the model and its declarations, then inspect named_model and reproduce the case through the conftest.py registration described in the issue. Done means a factory function can be used as the model while an explicit name such as my_model controls the registered fixture names, including prop1 and prop2.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.