Comments (8)
@olegpidsadnyi any thoughts on this? A sensible defacto way of retrieving build/unsaved models would replace a bunch of stuff in the suite I'm currently working on – and I'd rather not write a fixture for each model – is there a way to do this already or would adding a pre/suffixed fixture name be the way to go here?
from pytest-factoryboy.
A few further thoughts
- A decorator would be passable... but I'd rather make the default not saving than making people go to more /effort/ to write db-free tests
- Automatically toggling this based on whether the
db
fixture (or /a/ db fixture) is included would be neat but kind of crazy, and I have no idea how you'd do that... that'd negate the need for pre/suffixing though which would be cool - An alternative to the above could be a
nodb
fixture or something but again crazy no idea how
A suffix or a decorator to adjust how the factory works for the test in question seems like the way to go but I am wondering if we can do better
from pytest-factoryboy.
@stevelacey it is more like what kind of base factory are you using, right? What kind of ORM is it in your case. I guess there's a difference between build and create, but if you have a non-db model, what kind of difference does it make?
Could you please describe your setup?
from pytest-factoryboy.
from pytest-factoryboy.
from pytest-factoryboy.
This is very good idea. I try another case, byt this is not work. Normally in FactoryBoy we have strategy
in class Meta
. If I set BUILD_STRATEGY
and usage fixture_factory and call to them, this fixture connect with my db. This is not intuitive..
register(MyModel)
def test_something(my_model_factory):
my_model_factory() # <- this call db
my_model_factory.build(). # <- this not call db
from pytest-factoryboy.
from pytest-factoryboy.
Wouldn't the following work? Generate these fixtures:
@pytest.fixture
def derive_factory_strategy(): # or call it factory_strategy_strategy ;)
def derive(factory_class):
return factory_class._meta.strategy
return derive
@pytest.fixture
def {factory_name}__strategy(derive_factory_strategy):
strategy = derive_factory_strategy(factory_class)
...
@pytest.yield_fixture
def {factory_name}({factory_name}__strategy):
# - Setting TheFactory._meta.strategy is basically all use_strategy does.
# I'm not using it as I have to get the _meta.strategy so that
# I can reset it later and there's no public function for that that I know of.
# - You could also use the pytest builtin monkeypatch.setattr to temporarily
# change the strategy.
# (It's necessary to change it back as the factory is the actual factory
# class, which isn't isolated across tests)
default_strategy = foo_factory._meta.strategy
foo_factory._meta.strategy = {factory_name}__strategy
yield foo_factory
# yield_fixture catches exceptions for us, no need for try: finally:
foo_factory._meta.strategy = default_strategy
The user can now globally override the desired strategy, e.g. to derive from presence of a db fixture the user could write:
@pytest.fixture
def derive_factory_strategy(request):
def derive(factory_class):
if 'db' in request.fixturenames:
return factory.CREATE_STRATEGY
else:
return factory.BUILD_STRATEGY
If a user wants to override a single FooFactory's strategy, they can override the foo_factory__strategy
fixture.
from pytest-factoryboy.
Related Issues (20)
- Fix tests/test_postgen_dependencies::test_postgen_related
- AttributeError using `LazyFixture` in `register()` HOT 5
- Parent factory model name disregards `_name` override. HOT 1
- Ship tests with source distribution HOT 1
- function uses no argument HOT 1
- UserWarning: UserWarning: Using a <class 'list'> as model type for <ListFactory for <class 'list'>> is discouraged HOT 5
- Error `fixture not found` if define `register(_name=)` for connected SubFactory which is connected with another SubFactory HOT 1
- DeprecationWarning with pytest 7.2.0 HOT 4
- PytestAssertRewriteWarning
- Feature request: override name of factory fixture? HOT 3
- Alternative for `register`
- register with type hints HOT 3
- Improve handling of `factory.Maybe`
- Request other fixtures in fixtures generated by register HOT 3
- How can I pass function-scope fixtures (e.g. db sessions) to each Factory in combination with pytest-asyncio?
- Question regarding of usage of LazyFixture in @pytest.mark.parametrize on alias instead of chained fixture. HOT 1
- Broken support for Python 3.7 HOT 1
- 2.7.0: missing git version tag
- factory.SubFactory fixture collision when the model has the same name HOT 6
- `Trait`s not being considered by pytest_factoryboy
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pytest-factoryboy.