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