Method for generating test classes. Returns a dictionary where keys are the names of test classes and values are the test class objects.
(
test_path,
module=__name__,
class_name_prefix="",
expected_failures=[], # noqa: B006
bypass_test_generation_errors=False,
**kwargs,
)
| 1589 | |
| 1590 | |
| 1591 | def generate_test_classes( |
| 1592 | test_path, |
| 1593 | module=__name__, |
| 1594 | class_name_prefix="", |
| 1595 | expected_failures=[], # noqa: B006 |
| 1596 | bypass_test_generation_errors=False, |
| 1597 | **kwargs, |
| 1598 | ): |
| 1599 | """Method for generating test classes. Returns a dictionary where keys are |
| 1600 | the names of test classes and values are the test class objects. |
| 1601 | """ |
| 1602 | test_klasses = {} |
| 1603 | |
| 1604 | def test_base_class_factory(test_spec): |
| 1605 | """Utility that creates the base class to use for test generation. |
| 1606 | This is needed to ensure that cls.TEST_SPEC is appropriately set when |
| 1607 | the metaclass __init__ is invoked. |
| 1608 | """ |
| 1609 | |
| 1610 | class SpecTestBase(with_metaclass(UnifiedSpecTestMeta)): # type: ignore |
| 1611 | TEST_SPEC = test_spec |
| 1612 | EXPECTED_FAILURES = expected_failures |
| 1613 | |
| 1614 | base = SpecTestBase |
| 1615 | |
| 1616 | # Add "encryption" marker if the "csfle" runOnRequirement is set. |
| 1617 | for req in test_spec.get("runOnRequirements", []): |
| 1618 | if "csfle" in req: |
| 1619 | base = pytest.mark.encryption(base) |
| 1620 | |
| 1621 | return base |
| 1622 | |
| 1623 | found_any = False |
| 1624 | for dirpath, _, filenames in os.walk(test_path): |
| 1625 | dirname = os.path.split(dirpath)[-1] |
| 1626 | |
| 1627 | for filename in filenames: |
| 1628 | found_any = True |
| 1629 | fpath = os.path.join(dirpath, filename) |
| 1630 | with open(fpath) as scenario_stream: |
| 1631 | # Use tz_aware=False to match how CodecOptions decodes |
| 1632 | # dates. |
| 1633 | opts = json_util.JSONOptions(tz_aware=False) |
| 1634 | scenario_def = json_util.loads(scenario_stream.read(), json_options=opts) |
| 1635 | |
| 1636 | test_type = os.path.splitext(filename)[0] |
| 1637 | snake_class_name = "Test{}_{}_{}".format( |
| 1638 | class_name_prefix, |
| 1639 | dirname.replace("-", "_"), |
| 1640 | test_type.replace("-", "_").replace(".", "_"), |
| 1641 | ) |
| 1642 | class_name = snake_to_camel(snake_class_name) |
| 1643 | |
| 1644 | try: |
| 1645 | schema_version = Version.from_string(scenario_def["schemaVersion"]) |
| 1646 | mixin_class = _SCHEMA_VERSION_MAJOR_TO_MIXIN_CLASS.get(schema_version[0]) |
| 1647 | if mixin_class is None: |
| 1648 | raise ValueError( |
no test coverage detected