Code Monkey home page Code Monkey logo

Comments (12)

jnewb1 avatar jnewb1 commented on May 30, 2024 2

closed by: #115

from pytest-cpp.

nrbnlulu avatar nrbnlulu commented on May 30, 2024 1

just crafted this minimal thing

import json
import re
import subprocess
from functools import cached_property
from pathlib import Path
from typing import Optional

import pytest
from typing_extensions import TypedDict

build_dir = Path(__file__).parent.parent / "build"


class TestProperty(TypedDict):
    name: str
    value: str


class TestDefinition(TypedDict):
    name: str
    properties: list[TestProperty]


class CtestDiscoveryResult(TypedDict):
    tests: list[TestDefinition]


class CtestTestCommand:
    def __init__(self, test_name: str) -> None:
        self._data: list[str] = ["ctest", "-R"]
        self.test_name: str = test_name
        self.ret_res: Optional[subprocess.CompletedProcess] = None

    def add_command(self, command: str):
        self._data.append(command)

    @cached_property
    def failed_log(self) -> str | None:
        assert self.ret_res
        try:
            match = re.findall("(in: )(.*)(\\.log)", self.ret_res.stderr.decode())
            log_file = match[0][1]
        except IndexError:
            return None
        return Path(log_file + ".log").resolve(True).read_text()

    def run(self) -> None:
        self.ret_res = subprocess.run(
            [*self._data, self.test_name],
            cwd=build_dir.resolve(True),
            capture_output=True,
        )


def collect_tests() -> list[CtestTestCommand]:
    ret: list[CtestTestCommand] = []
    res = subprocess.run(
        ["ctest", "--show-only=json-v1"],
        cwd=build_dir.resolve(True),
        capture_output=True,
    )
    data: CtestDiscoveryResult = json.loads(res.stdout)
    for test in data["tests"]:
        ret.append(CtestTestCommand(test["name"]))
    return ret


test_commands = collect_tests()


@pytest.mark.parametrize("command", test_commands, ids=lambda v: v.test_name)
def test_cpp(command: CtestTestCommand):
    command.run()
    if log_file := command.failed_log:
        pytest.fail(msg=f"\n {'-'*8} Test {command.test_name} Failed {'-'*8} \n {log_file}")

Just point it to the build dir and it would do the rest.

from pytest-cpp.

bluecube avatar bluecube commented on May 30, 2024 1

Wow you're fast :) . Thank you. I hope I'll be able to try it out later today.

from pytest-cpp.

nicoddemus avatar nicoddemus commented on May 30, 2024 1

No worries, but I like to mention that to encourage someone interested in Catch2 v3 to contribute.

from pytest-cpp.

nicoddemus avatar nicoddemus commented on May 30, 2024 1

Should I add Catch2v3Facade, or add it to the existing one?

Whichever is more convenient: if the changes are small and localized, might make sense to add to the existing facade, however if you need to change every single method to do something different, it might make more sense to write a new facade.

from pytest-cpp.

bluecube avatar bluecube commented on May 30, 2024 1

That might be true, but I'm using it with SCons.

from pytest-cpp.

nrbnlulu avatar nrbnlulu commented on May 30, 2024

Does your tests gets collected?

from pytest-cpp.

bluecube avatar bluecube commented on May 30, 2024

No, it doesn't.

from pytest-cpp.

nicoddemus avatar nicoddemus commented on May 30, 2024

If it works, happy to review/merge a PR. 👍

from pytest-cpp.

nrbnlulu avatar nrbnlulu commented on May 30, 2024

@nicoddemus I don't use any library code though, I just find tests and run them via parameterize
something close to what I suggested in #20

from pytest-cpp.

bluecube avatar bluecube commented on May 30, 2024

@nrbnlulu Your code didn't work for me directly, because I don't use ctest in my project, I needed to call the test binary directly. But it gave me some starting point and I ended up modifying it for my use case ( https://github.com/bluecube/tablog/blob/87506b3b69a873914d6ea726facf4c6c4bf752ec/encoder/tests/test_adapter.py ), so thank you for the push in the right-ish direction :)

I'd still like to have more complete support for v3, though, including specific failed assertions and random generator seed, so I'll try to make a PR.

Should I add Catch2v3Facade, or add it to the existing one?

from pytest-cpp.

nrbnlulu avatar nrbnlulu commented on May 30, 2024

@bluecube AFAIK if you use catch with Cmake it generates ctest binary for you am I wrong?

from pytest-cpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.