Quickstart

The basic implementation is a decorator that accepts arbitrary keyword arguments:

from pytraceability.common import traceability

@traceability('REQUIREMENTS_ID',something_else='value')
def my_function():
    pass

You can define custom decorators to implement your own traceability requirements.

from pytraceability.common import traceability
from pytraceability.config import PROJECT_NAME


class pytraceability(traceability):
    def __init__(self, key: str, *, info: str):
        REQUIRED_PREFIX = f"{PROJECT_NAME.upper()}-"
        if not key.startswith(REQUIRED_PREFIX):
            raise ValueError(f"Key {key} does not start with {REQUIRED_PREFIX}")
        super().__init__(key, info=info)

What sort of things might you want to do with this?

  • Enforce specific formats for the traceability keys

  • Enforce specific information in the metadata (eg JIRA ticket numbers, etc)

  • Enforce a ‘category’ tag and ensure it takes one of a fixed set of values

Once you’ve defined your custom decorator, you can start using them in the codebase.

from pytraceability.common import traceability


@traceability(
    "EXAMPLE-1",
    info="As an example, I might want to track the implementation "
    "of this function, and link it to a requirement.",
    requirement="JIRA-1234",
)
def example():
    return "An exciting value"

You can then extract this decorator either programmatically

>>> from pytraceability.collector import PyTraceabilityCollector
>>> from pytraceability.config import PyTraceabilityConfig
>>> config = PyTraceabilityConfig(base_directory='example', output_format='json')
>>> pprint(json.loads(list(PyTraceabilityCollector(config).get_printable_output())[0]))
{'reports': [{'contains_raw_source_code': False,
              'end_line_number': 11,
              'file_path': 'example/example.py',
              'function_name': 'example',
              'history': None,
              'key': 'EXAMPLE-1',
              'line_number': 10,
              'metadata': {'info': 'As an example, I might want to track the '
                                   'implementation of this function, and link '
                                   'it to a requirement.',
                           'requirement': 'JIRA-1234'},
              'source_code': 'def example():\n    return "An exciting value"'}]}

Or via the command line:

$ pytraceability --base-directory example --output-format key-only --decorator-name=traceability
EXAMPLE-1