Skip to content

Commit 386062d

Browse files
committed
Pull test runners into classes
1 parent b0fd195 commit 386062d

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

TestRunner/GenericTestRunner.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
import pytest
4+
5+
from pytest_plugins import ResultsCollector, SessionStartPlugin
6+
7+
8+
class GenericTestRunner(metaclass=ABCMeta):
9+
@abstractmethod
10+
def run(self, *args, **kwargs) -> (int, str): pass
11+
12+
import subprocess
13+
14+
15+
class SubProcessTestRunner(GenericTestRunner):
16+
code_dir: str
17+
test_dir: str
18+
19+
def __init__(self, _code, _test) -> None:
20+
self.code_dir = _code
21+
self.test_dir = _test
22+
23+
def run(self, *args, **kwargs) -> (int, str):
24+
# TODO: check that code_dir and test_dir exist
25+
proc = subprocess.run(["pytest", self.test_dir], capture_output=True)
26+
return proc.returncode, proc.stdout
27+
28+
29+
class InlineTestRunner(GenericTestRunner):
30+
31+
def __init__(self, _code, _test) -> None:
32+
self.code_dir = _code
33+
self.test_dir = _test
34+
35+
def run(self, *args, **kwargs) -> (int, str):
36+
collector = ResultsCollector()
37+
setup = SessionStartPlugin()
38+
pytest.main(args=["-k", "ExampleClass"], plugins=[collector, setup])
39+
_out = ""
40+
41+
if collector.exitcode > 0:
42+
for report in collector.reports:
43+
_out += f"{report.outcome.upper()} {report.nodeid} ... Outcome: - {report.longrepr.reprcrash.message}"
44+
_out += "\n"
45+
_out += report.longreprtext
46+
_out += "\n"
47+
return collector.exitcode, _out

TestRunner/SubProcessTestRunner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import GenericTestRunner

TestRunner/__init__.py

Whitespace-only changes.

main.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import os
22
import typer
33

4-
# for the test runner
5-
import pytest
6-
# ------------------
7-
84
import langroid as lr
95
from langroid.utils.configuration import set_global, Settings
106
from langroid.utils.logging import setup_colored_logging
117

12-
from pytest_plugins import ResultsCollector, SessionStartPlugin
8+
from TestRunner.GenericTestRunner import InlineTestRunner
139

1410
app = typer.Typer()
1511
setup_colored_logging()
@@ -40,21 +36,6 @@ def generate_first_attempt() -> None:
4036
_out.write(response.content)
4137

4238

43-
def get_test_results() -> str:
44-
collector = ResultsCollector()
45-
setup = SessionStartPlugin()
46-
pytest.main(args=["-k", "ExampleClass"], plugins=[collector, setup])
47-
_out = ""
48-
49-
if collector.exitcode > 0:
50-
for report in collector.reports:
51-
_out += f"{report.outcome.upper()} {report.nodeid} ... Outcome: - {report.longrepr.reprcrash.message}"
52-
_out += "\n"
53-
_out += report.longreprtext
54-
_out += "\n"
55-
return collector.exitcode, _out
56-
57-
5839
def generate_next_attempt(test_results: str) -> None:
5940
cfg = lr.ChatAgentConfig(
6041
llm=lr.language_models.OpenAIGPTConfig(
@@ -100,8 +81,11 @@ def teardown() -> None:
10081

10182
def chat() -> None:
10283
generate_first_attempt()
84+
test_runner = InlineTestRunner("", os.path.join(".", "test"))
10385
for _ in range(5):
104-
test_exit_code, test_results = get_test_results()
86+
# test_exit_code, test_results = get_test_results()
87+
test_exit_code, test_results = test_runner.run()
88+
print(test_results)
10589
if test_exit_code == 1:
10690
generate_next_attempt(test_results)
10791
else:

0 commit comments

Comments
 (0)