Skip to content

Commit ae3f79c

Browse files
authored
tests: add meaningful names to validation tests (#588)
When packaging cyclonedx-python-lib for a Linux distribution, it’s pretty common that some JSON validation tests fail. [1] Due to the large number of combinations and the fact that these tests are consecutively numbered, it has been tedious to figure out which tests are exactly failing and why. This in turn makes it difficult to decide which tests to disable or report upstream. Append meaningful names to validation tests so that instead of e.g.: […]::TestJsonValidator::test_validate_no_none_001 […]::TestJsonValidator::test_validate_no_none_002 […]::TestJsonValidator::test_validate_no_none_003 […]::TestJsonValidator::test_validate_no_none_004 […]::TestJsonValidator::test_validate_no_none_005 […]::TestJsonValidator::test_validate_no_none_006 […]::TestJsonValidator::test_validate_no_none_007 […]::TestJsonValidator::test_validate_no_none_008 the tests are named: […]::TestJsonValidator::test_validate_no_none_001_valid_component_swid_1_6 […]::TestJsonValidator::test_validate_no_none_002_valid_machine_learning_considerations_env_1_6 […]::TestJsonValidator::test_validate_no_none_003_valid_metadata_tool_1_6 […]::TestJsonValidator::test_validate_no_none_004_valid_patch_1_6 […]::TestJsonValidator::test_validate_no_none_005_valid_empty_components_1_6 […]::TestJsonValidator::test_validate_no_none_006_valid_properties_1_6 […]::TestJsonValidator::test_validate_no_none_007_valid_service_1_6 […]::TestJsonValidator::test_validate_no_none_008_valid_metadata_author_1_6 [1]: https://aur.archlinux.org/cgit/aur.git/diff/PKGBUILD?h=python-cyclonedx-lib&id=9c6ae556874a633a521407a77a9a85bb31da2047 Signed-off-by: Claudia <[email protected]>
1 parent a498faa commit ae3f79c

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

tests/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
# Copyright (c) OWASP Foundation. All Rights Reserved.
1717
import re
1818
from os import getenv, path
19-
from os.path import join
20-
from typing import TYPE_CHECKING, Any, Generator, Iterable, List, Optional, TypeVar, Union
19+
from os.path import basename, join, splitext
20+
from typing import TYPE_CHECKING, Any, Generator, Iterable, List, Optional, Tuple, TypeVar, Union
2121
from unittest import TestCase
2222
from uuid import UUID
2323

@@ -183,3 +183,10 @@ def is_valid_for_schema_version(purpose: Union[Any], sv: SchemaVersion) -> bool:
183183

184184
def mksname(purpose: Union[Any], sv: SchemaVersion, f: OutputFormat) -> str:
185185
return f'{_get_purpose_as_str(purpose)}-{sv.to_version()}.{_SNAME_EXT[f]}'
186+
187+
188+
class DpTuple(Tuple[SchemaVersion, str]):
189+
@property
190+
def __name__(self) -> str:
191+
schema_version, test_data_file = self
192+
return f'{schema_version.to_version()}-{splitext(basename(test_data_file))[0]}'

tests/test_validation_json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
from cyclonedx.exception import MissingOptionalDependencyException
2626
from cyclonedx.schema import OutputFormat, SchemaVersion
2727
from cyclonedx.validation.json import JsonStrictValidator, JsonValidator
28-
from tests import SCHEMA_TESTDATA_DIRECTORY
28+
from tests import SCHEMA_TESTDATA_DIRECTORY, DpTuple
2929

3030
UNSUPPORTED_SCHEMA_VERSIONS = {SchemaVersion.V1_0, SchemaVersion.V1_1, }
3131

3232

3333
def _dp(prefix: str) -> Generator:
3434
return (
35-
(sv, tf) for sv in SchemaVersion if sv not in UNSUPPORTED_SCHEMA_VERSIONS
35+
DpTuple((sv, tf)) for sv in SchemaVersion if sv not in UNSUPPORTED_SCHEMA_VERSIONS
3636
for tf in iglob(join(SCHEMA_TESTDATA_DIRECTORY, sv.to_version(), f'{prefix}-*.json'))
3737
)
3838

tests/test_validation_xml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
from cyclonedx.exception import MissingOptionalDependencyException
2626
from cyclonedx.schema import OutputFormat, SchemaVersion
2727
from cyclonedx.validation.xml import XmlValidator
28-
from tests import SCHEMA_TESTDATA_DIRECTORY
28+
from tests import SCHEMA_TESTDATA_DIRECTORY, DpTuple
2929

3030
UNSUPPORTED_SCHEMA_VERSIONS = set()
3131

3232

3333
def _dp(prefix: str) -> Generator:
3434
return (
35-
(sv, tf) for sv in SchemaVersion if sv not in UNSUPPORTED_SCHEMA_VERSIONS
35+
DpTuple((sv, tf)) for sv in SchemaVersion if sv not in UNSUPPORTED_SCHEMA_VERSIONS
3636
for tf in iglob(join(SCHEMA_TESTDATA_DIRECTORY, sv.to_version(), f'{prefix}-*.xml'))
3737
)
3838

0 commit comments

Comments
 (0)