pytest-dev / pytest-dev/pytest-factoryboy
Using `factory.List` resolves in unusable fixtures
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 400
- Forks
- 44
- Avg merge
- 6h 34m
- Merged PRs (30d)
- 1
Description
Setup
Here are my models:
from typing import List
from mypy_extensions import TypedDict
class WakatimeCommit(TypedDict):
"""Class to represent a single Wakatime commit instance from API. """
hash: str
author_email: str
class WakatimeCommitsResponse(TypedDict):
"""Class to represent Wakatime response from commits API."""
commits: List[WakatimeCommit]
And my factories:
import factory
from pytest_factoryboy import register
@register
class WakatimeCommitFactory(factory.BaseDictFactory):
"""
Fake factory for a single Wakatime commit entry.
Note:
This class is marked as protected and is not registered,
since we do not actually need a single commit in tests.
"""
class Meta(object):
model = WakatimeCommit
hash = '123abc'
author_email = 'some@email.com'
@register
class WakatimeCommitsResponseFactory(factory.BaseDictFactory):
"""Fake factory for Wakatime `/commits` response."""
class Meta(object):
model = WakatimeCommitsResponse
commits = factory.List([
factory.SubFactory(WakatimeCommitFactory) for _ in range(10)
])
And finally my test:
def test_pipeline_for_opened_valid_mr(
wakatime_commits_response,
):
"""
Tests the whole process of adding spent time to a merge request.
All requests are mocked, everything returns valid responses.
"""
print(wakatime_commits_response)
assert 1 == 2
Error
This tests fails due to unresolved fixtures:
def test_pipeline_for_opened_valid_mr(
file <string>, line 2: source code not available
file <string>, line 2: source code not available
E fixture 'list' not found
> available fixtures:
...
wakatime_commit, wakatime_commit__author_email, wakatime_commit__hash, wakatime_commit__total_seconds, wakatime_commit_factory, wakatime_commits_response, wakatime_commits_response__commits, wakatime_commits_response_factory
...
I guess this happens due to the fact that I am using factory.List.
Workaround
class WakatimeCommitsResponseFactory(factory.BaseDictFactory):
...
@factory.post_generation
def commits(self, create, extracted, **kwargs):
if extracted:
self['commits'] = extracted
else:
self['commits'] = []
for _ in range(10):
self['commits'].append(WakatimeCommitFactory.build())
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example using WakatimeCommitsResponseFactory, factory.List, and pytest_factoryboy's registered fixtures, then inspect how the commits declaration is resolved. Verify the behavior against the shown fixture error and workaround; done means factory.List no longer exposes an unusable list fixture while the response still contains the expected generated commits.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100