'pytest: Run general test with dynamic input from files
I'm currently trying to write a python test module with pytest which reads several different information from a bunch of files. These informations shall be used as inputs (fixtures) for 2 general tests. These 2 tests shall be run for each fixture (so for each file).
What I want are dynamic fixtures generated from the content of an undefined amount of text files (jsons).
Then I have two pytest "test_*" functions which are fed with this undefined amount of fixtures.
Is such a thing possible with pytest? I already tried the lib pytest-cases, but I were unable to achieve my desired solution.
That is my current code base, where I want the yield to return a "for each *.json" fixture:
from pytest_cases import parametrize_with_cases, get_case_id
class Foo:
def matching_events(self):
# - glob for *.json files
# - read the rule files and events
# generate the fixture objects and return them for each *.json file
yield "test_rule_name", {}, {}
def mismatching_events(self):
# - glob for *.json files
# - read the rule files and events
# generate the fixture objects and return them for each *.json file
yield "test_rule_name", {}, {}
def case_id_generator(case_fun):
"""Custom test case id"""
return "#%s#" % case_fun
@parametrize_with_cases("rule_name, rule_definition, events", cases=Foo, prefix="matching_", ids=case_id_generator)
def test_rule_match(rule_name, rule_definition, events):
assert isinstance(rule_name, str)
assert isinstance(rule_definition, dict)
assert isinstance(events, dict)
# do some more things with the rule definition and evnets
@parametrize_with_cases("rule_name, rule_definition, events", cases=Foo, prefix="mismatching_", ids=case_id_generator)
def test_rule_mismatch(rule_name, rule_definition, events):
assert isinstance(rule_name, str)
assert isinstance(rule_definition, dict)
assert isinstance(events, dict)
# do some more things with the rule definition and evnets
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
