-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Catch2 v3 #102
Comments
Does your tests gets collected? |
No, it doesn't. |
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. |
Wow you're fast :) . Thank you. I hope I'll be able to try it out later today. |
If it works, happy to review/merge a PR. 👍 |
@nicoddemus I don't use any library code though, I just find tests and run them via parameterize |
No worries, but I like to mention that to encourage someone interested in Catch2 v3 to contribute. |
@nrbnlulu Your code didn't work for me directly, because I don't use 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 |
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. |
@bluecube AFAIK if you use catch with Cmake it generates ctest binary for you am I wrong? |
That might be true, but I'm using it with SCons. |
closed by: #115 |
As far as I can tell, v3 binaries don't work with the current Catch2 support, because
--list-test-names-only
command line option is missing.The text was updated successfully, but these errors were encountered: