-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from olegpidsadnyi/lazyfixture
Fixture attributes implemented
- Loading branch information
Showing
7 changed files
with
191 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""pytest-factoryboy pytest hooks.""" | ||
|
||
|
||
def pytest_factoryboy_done(request): | ||
"""Called after all factory based fixtures and their post-generation actions were evaluated.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"""pytest-factoryboy plugin.""" | ||
|
||
import pytest | ||
|
||
|
||
class Request(object): | ||
|
||
"""PyTest FactoryBoy request.""" | ||
|
||
def __init__(self): | ||
self.deferred = [] | ||
self.is_evaluated = False | ||
|
||
def defer(self, function): | ||
"""Defer post-generation declaration execution until the end of the test setup. | ||
:param function: Function to be deferred. | ||
:note: Once already evaluated all following defer calls will execute the function directly. | ||
""" | ||
if self.is_evaluated: | ||
function() | ||
else: | ||
self.deferred.append(function) | ||
|
||
def evaluate(self): | ||
"""Finalize, run deferred post-generation actions, etc.""" | ||
while True: | ||
try: | ||
self.deferred.pop(0)() | ||
except IndexError: | ||
return | ||
finally: | ||
self.is_evaluated = True | ||
|
||
|
||
@pytest.fixture | ||
def factoryboy_request(): | ||
"""PyTest FactoryBoy request fixture.""" | ||
return Request() | ||
|
||
|
||
@pytest.mark.tryfirst | ||
def pytest_runtest_call(item): | ||
"""Before the test item is called.""" | ||
try: | ||
request = item._request | ||
except AttributeError: | ||
# pytest-pep8 plugin passes Pep8Item here during tests. | ||
return | ||
request.getfuncargvalue("factoryboy_request").evaluate() | ||
request.config.hook.pytest_factoryboy_done(request=request) | ||
|
||
|
||
def pytest_addhooks(pluginmanager): | ||
"""Register plugin hooks.""" | ||
from pytest_factoryboy import hooks | ||
pluginmanager.addhooks(hooks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters