-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutation_tester.py
73 lines (60 loc) · 2.28 KB
/
mutation_tester.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import subprocess
import abc
class MutationRunner(abc.ABC):
"""
Defines an interface for running mutation testing.
"""
def __init__(self, target_module, test_module):
self._target_module = target_module
self._test_module = test_module
@abc.abstractmethod
def run(self):
pass
# todo: implement a MutPyRunner that is not dependent on the command line
class MutPyRunner(MutationRunner):
"""
Defines a runner for MutPy mutation testing tool.
"""
def __init__(self, target_module, test_module):
self._target_module = self._convert_path_to_module(target_module)
self._test_module = self._convert_path_to_module(test_module)
# mut.py --target examples.example --unit-test examples.example_test -m
self._command = [
"mut.py",
"--target",
self._target_module,
"--unit-test",
self._test_module,
"--runner",
"unittest",
]
def _convert_path_to_module(self, path):
"""
Convert a file path to a module path.
"""
return path.replace("./", "").replace("/", ".").replace(".py", "")
# Pretty naive implementation. Should be improved. Definitely not alright to run mut py from the command line.
def run(self):
try:
process = subprocess.Popen(
self._command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
while True:
if process.stdout is None:
break
output = process.stdout.readline()
if output == "" and process.poll() is not None:
break
if output:
print(output.strip())
stderr = process.communicate()[1]
if stderr:
print(stderr.strip())
if process.returncode != 0:
raise subprocess.CalledProcessError(process.returncode, self._command)
except subprocess.CalledProcessError as error:
raise RuntimeError(f"An error occurred while running MutPy: {error}")
except FileNotFoundError:
raise RuntimeError(
"MutPy command not found. Make sure it's installed and in your PATH."
)