network-unit-testing-system / nuts

NUTS defines a desired network state and checks it against a real network using pytest and nornir.
https://nuts.readthedocs.io/
MIT License
106 stars 5 forks source link

Add hook pytest_nuts_sigle_result #94

Closed ubaumann closed 5 months ago

ubaumann commented 5 months ago

Allowing to customize reports like junit xml without hard code it in Nuts

ubaumann commented 5 months ago

I want to allow uses to extend reports like junit without hard code it in nuts

As a PoC I created the following conftest.py

import pytest

@pytest.hookimpl(tryfirst=True)
def pytest_nuts_single_result(request, nuts_ctx, result):
    test_extras = nuts_ctx.nuts_parameters.get("test_extras", {})
    for p_name, p_value in test_extras.get("properties", {}).items():
        request.node.user_properties.append((p_name, p_value))

And with the test file:

- test_module: tests.showcase.showcase_expanse
  test_class: TestExpanseCrew
  test_extras:
    properties:
      demo: urs
      demo2: 1
  test_data:
    - ship: rocinante
      name: "naomi nagata"
      role: engineer
      origin: belter

pytest tests/showcase_test/test-expanse.yaml --junit-xml=mydemo.xml generates:

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
    <testsuite name="pytest" errors="0" failures="0" skipped="0" tests="15" time="0.050"
        timestamp="2024-05-27T08:43:13.722041" hostname="codespaces-d1d96a">
        <testcase classname="tests.showcase_test.test-expanse.yaml.TestExpanseCrew"
            name="test_name[0]" file="tests/showcase/showcase_expanse.py" line="79" time="0.000">
            <properties>
                <property name="demo" value="urs" />
                <property name="demo2" value="1" />
            </properties>
        </testcase>

@The-Compiler before I continue, is this an acceptable approach?

The-Compiler commented 5 months ago

Seems fine by me!