File size: 1,541 Bytes
1afb40b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import pytest
from pathlib import Path
from src.tools.executor import TestExecutor

def test_executor_nonexistent_target(tmp_path):
    executor = TestExecutor(repo_root=str(tmp_path))
    res = executor.run_pytest(test_path="nonexistent_test.py")
    assert res["status"] == "error"
    assert "does not exist" in res["message"]

def test_executor_run_valid_suite(tmp_path):
    # Create a dummy valid test file
    dummy_test = tmp_path / "test_dummy.py"
    dummy_test.write_text("def test_pass(): assert True\n", encoding="utf-8")
    
    executor = TestExecutor(repo_root=str(tmp_path))
    res = executor.run_pytest(test_path="test_dummy.py", extra_args=["-v"])
    assert res["status"] == "passed"
    assert res["exit_code"] == 0

def test_executor_timeout_mock(monkeypatch, tmp_path):
    executor = TestExecutor(repo_root=str(tmp_path))
    
    import subprocess
    def mock_run(*args, **kwargs):
        raise subprocess.TimeoutExpired(cmd="pytest", timeout=60)
    
    monkeypatch.setattr(subprocess, "run", mock_run)
    res = executor.run_pytest()
    assert res["status"] == "error"
    assert "timed out" in res["message"]

def test_executor_exception_mock(monkeypatch, tmp_path):
    executor = TestExecutor(repo_root=str(tmp_path))
    
    import subprocess
    def mock_run(*args, **kwargs):
        raise RuntimeError("Unexpected failure")
    
    monkeypatch.setattr(subprocess, "run", mock_run)
    res = executor.run_pytest()
    assert res["status"] == "error"
    assert "Unexpected failure" in res["message"]