Coverage for src/renaissance/recipes/unit_to_pytest.py: 97%
178 statements
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-16 16:04 +0000
« prev ^ index » next coverage.py v7.17.0, created at 2026-11-16 16:04 +0000
1# Copyright (C) 2024-2026 TNO
2#
3# Licensed under the terms specified in the LICENSE file in the root directory
4# of this repository, also available at:
5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE
7"""AI: Recipe that converts unittest-style test files to pytest style."""
9import textwrap
10from collections.abc import Sequence
11from pathlib import Path
13from renaissance.integrations.python.ast.util import convert_function
14from renaissance.recipes.python_refactoring import PythonRefactoring
15from renaissance.syntax_tree import PatternMatch
16from renaissance.syntax_tree.ast_finder import find_semantic_kind
17from renaissance.syntax_tree.match_finder import match_pattern
18from renaissance.syntax_tree.node_protocol import NodeProtocol
19from renaissance.syntax_tree.semantic_kind import SemanticKind
22# TODO(FrancescoPezzella): run() leaves broken or non-terminating output for common unittest files.
23# https://github.com/TNO/Renaissance.Py/issues/317
24class UnitToPytest(PythonRefactoring):
25 """AI: Recipe that converts unittest-style test files to pytest style."""
27 def __init__(self, file) -> None:
28 """Hide internal administration in the parent class so that this class you only deals with specific refactors."""
29 super().__init__(file)
30 self.black_list_pattern = "utils_for_test"
31 self.white_list_pattern = "test"
33 def run(self) -> None:
34 """Entry point for converting unittest to pytest."""
35 self.refactor()
37 self.post_processing()
39 def refactor(self) -> None:
40 """AI: Apply file-, class-, and function-level unittest-to-pytest conversions."""
41 # 1: file level changes
42 self.convert_test_class()
43 self.restructure_module()
44 self.replace_stmt("unittest.main()", "pytest.main()")
45 self.replace_stmt("import unittest", "import pytest\nfrom hamcrest import *")
46 self.replace_stmt("from parameterized import parameterized", "import pytest\nfrom hamcrest import *")
47 self.replace_stmt("from unittest import TestCase,$$symbols", "import pytest\nfrom hamcrest import *")
48 self.replace_stmt("from unittest import TestCase", "import pytest\nfrom hamcrest import *")
50 # 2: class level changes
51 self.convert_parameterized_test()
52 self.convert_test_setup()
53 self.commit()
55 # 3: function level changes
57 self.convert_skip_test()
58 self.remove_print()
59 self.convert_plain_assert_same_length()
60 self.commit()
61 self.replace_stmt("assert $stmt, $$msg", "assert_that($stmt, is_(True), $$msg)")
62 self.replace_stmt("self.assertTrue($exp,$$msg)", "assert_that($exp, is_(True), $$msg)")
63 self.replace_stmt("self.assertFalse($exp, $$msg)", "assert_that($exp, is_(False), $$msg)")
65 self.convert_assert("self.assertEqual($exp, $act)", "assert_that($exp, is_($act))")
66 self.convert_assert("self.assertGreaterEqual($exp, $act)", "assert_that($exp, greater_than_or_equal_to($act))")
67 self.convert_assert("self.assertGreater($exp, $act)", "assert_that($exp, greater_than($act))")
68 self.convert_assert("self.assertLesserEqual($exp, $act)", "assert_that($exp, less_than_or_equal_to($act))")
69 self.convert_assert("self.assertLesser($exp, $act)", "assert_that($exp, less_than($act))")
70 self.convert_assert("self.assertMultiLineEqual($act, $exp)", "assert_that($act, is_($exp))")
72 self.replace_stmt("self.assertIn($act, $exp)", "assert_that($exp, contain_string($act))")
73 self.replace_stmt("self.assertIsInstance($act, $exp)", "assert_that($act, is_($exp))")
74 self.replace_stmt("with self.assertRaises($exc): $call()", "assert_that(calling($call), raises($exc))")
76 def post_processing(self) -> None:
77 """AI: Repeatedly simplify assert_that(...) expressions until no further changes occur."""
78 # 4: improve to more concise asserts
79 while self.has_changed():
80 self.commit()
81 self.replace_stmt("assert_that($exp)", "assert_that($exp, is_(True))")
82 self.replace_stmt("assert_that(isinstance($exp, $act))", "assert_that($exp, is_($act))")
83 self.replace_stmt("assert_that(len($exp), $act)", "assert_that($exp, has_length($act))")
84 self.replace_stmt("assert_that(len($exp) >= 1)", "assert_that($exp, is_not(empty()))")
85 self.replace_stmt("assert_that(len($exp) >= 1, is_(True))", "assert_that($exp, is_not(empty()))")
86 self.replace_stmt("assert_that(len($exp) == $length)", "assert_that($exp, has_length($length))")
87 self.replace_stmt("assert_that($exp == $act)", "assert_that($exp, is_($act), $$msg)")
88 self.replace_stmt("assert_that($exp == $act, is_(True), $$msg)", "assert_that($exp, is_($act), $$msg)")
89 self.replace_stmt("assert_that(not $stmt, is_(True), $$msg)", "assert_that($stmt, is_(False) ,$$msg)")
90 self.replace_stmt("assert_that($stmt, is_not(True), $$msg)", "assert_that($stmt, is_(False) ,$$msg)")
91 self.replace_stmt("assert_that(not $stmt)", "assert_that($stmt, is_(False))")
92 self.replace_stmt("assert_that($el in $col, is_(True))", "assert_that($col, contains_exactly($el))")
93 self.replace_stmt("assert_that($exp, has_length(is_($act)))", "assert_that($exp, has_length($act))")
94 self.swap_expected_and_actual()
95 self.replace_stmt("assert_that(not $stmt)", "assert_that($stmt, is_(False))")
96 self.replace_stmt("assert_that($exp.startswith($act))", "assert_that($exp, starts_with($act))")
97 self.remove_duplicate_import("import pytest\nfrom hamcrest import *")
98 self.commit()
100 def convert_test_class(self) -> None:
101 """AI: Rewrite TestCase-derived class headers to drop the unittest base class."""
102 test_main: Sequence[NodeProtocol] = self.pattern_factory.create_statements(
103 "class $klass($test_class):\n $$test_cases\n",
104 ) # type: ignore[assignment]
105 for match in match_pattern(self.root.children, test_main):
106 klass = match["$klass"]
107 test_class = match["$test_class"]
109 if test_class.endswith("TestCase"):
110 # class inherit from TestCase (or unittest.TestCase)
111 if klass.endswith("Test"):
112 # class name ends with Test, rename by move Test to front
113 repl = match.signature.replace(f"{klass}({test_class}):", f"Test{klass[:-4]}:")
114 else:
115 # we assume there are only 2 variant TestExample and ExampleTest
116 repl = match.signature.replace(f"({test_class}):", ":")
118 # repl = f'class {match.expansions["$klass"][0]}:\n{raw(match.expansions["$$test_cases"])}'
119 self.replace(repl, match.nodes, False, False)
121 def convert_test_setup(self) -> None:
122 """AI: Convert a setUp method into a pytest autouse fixture named setup."""
123 setup_function = self.pattern_factory.create_statements("def setUp(self): $$stmts")
124 for match in match_pattern(self.body, setup_function):
125 # add decorator to the setup dunction and convert to snake case
126 repl = f"@pytest.fixture(autouse=True)\n{match.signature}".replace(" setUp(self)", " setup(self)")
127 self.replace(repl, match.nodes, False, False)
129 def convert_assert(self, pattern, replacement) -> None:
130 """AI: Replace calls matching pattern with replacement, swapping expected/actual arguments as needed."""
131 pat = self.pattern_factory.create_statements(pattern)
132 for match in match_pattern(self.root.children, pat):
133 repl = replacement
134 if self.is_swapped(match):
135 exp = match["$act"]
136 act = match["$exp"]
137 else: # original is wrong
138 act = match["$act"]
139 exp = match["$exp"]
140 repl = repl.replace("$exp", exp).replace("$act", act)
141 self.replace(repl, match.nodes, False, False)
143 def is_swapped(self, match: PatternMatch) -> bool:
144 """AI: Return whether $exp and $act appear swapped in the match (i.e. $exp is a literal)."""
145 return match.expansions["$exp"][0].semantic_kind is SemanticKind.LITERAL
147 def convert_parameterized_test(self) -> None:
148 """AI: Convert @parameterized.expand-decorated test functions into @pytest.mark.parametrize."""
149 for returns in ("", " -> $ret"):
150 unittest = self.pattern_factory.create_statements(
151 textwrap.dedent(f"""
152 @parameterized.expand($$parameters)
153 @$$decorator
154 def $fun($$args, *$$varg){returns}:
155 $$stmts
156 """),
157 )
158 for match in match_pattern(self.root.children, unittest):
159 self._convert_parameterized_match(match)
161 def _convert_parameterized_match(self, match: PatternMatch) -> None:
162 fun = match.nodes[0]
163 names: list[str] = [arg.node.arg for arg in match.expansions["$$args"]]
164 names = [name for name in names if name != "self"]
165 if varg := match.expansions["$$varg"]:
166 names.append(f"*{varg[0].signature}")
167 quoted = [f'"{name}"' for name in names]
168 # A single name stays a string; multiple names become a tuple (Ruff PT006).
169 args = quoted[0] if len(quoted) == 1 else f"({', '.join(quoted)})"
170 repl = fun.signature
171 if " def " in repl:
172 repl = repl.replace("@parameterized.expand(", f" @pytest.mark.parametrize({args},")
173 repl = repl.replace("@unittest.skip(", "@pytest.mark.skip(")
174 repl = textwrap.dedent(repl)
175 else:
176 repl = repl.replace("@parameterized.expand(", f"@pytest.mark.parametrize({args},")
177 repl = repl.replace("@unittest.skip(", "@pytest.mark.skip(")
179 self.replace(repl, fun, False, False)
181 def remove_print(self) -> None:
182 """AI: Remove print(...) statements, or their containing block if it's the only statement."""
183 print_msg = self.pattern_factory.create_statements("print($$msg)") # type: ignore[assignment]
184 for match in match_pattern(self.root.children, print_msg):
185 if len(match.nodes[0].parent.parent.body) == 1:
186 self.remove([match.nodes[0].parent.parent], False, False)
187 else:
188 self.remove(match.nodes, False, False)
190 def convert_plain_assert_same_length(self) -> None:
191 """AI: Replace a manual length-check assert with an assert_that(...) has_length assertion."""
192 for comparison in ("$exp == $var", "$var == $exp"):
193 pattern: Sequence[NodeProtocol] = self.pattern_factory.create_statements(
194 f'$var: int = len($real)\nassert {comparison}, "$var = " + str($var)',
195 )
196 for match in match_pattern(self.body, pattern):
197 repl = 'assert_that($real, has_length($exp), f"length of $real = {len($real)}")'
198 real = match["$real"]
199 repl = repl.replace("$exp", match["$exp"]).replace("$real", real)
200 self.replace(repl, match.nodes, False, False)
202 def convert_skip_test(self) -> None:
203 """AI: Replace unittest.skip attribute references with pytest.mark.skip."""
204 nodes = find_semantic_kind(self.root, SemanticKind.ATTRIBUTE)
205 for node in nodes:
206 if node.signature == "unittest.skip":
207 self.replace("pytest.mark.skip", node, False, False)
209 def swap_expected_and_actual(self) -> None:
210 """AI: Swap the $exp and $act arguments of assert_that(...) calls when they appear reversed."""
211 pattern: Sequence[NodeProtocol] = self.pattern_factory.create_statements("assert_that($exp, is_($act))") # type: ignore[assignment]
212 for match in match_pattern(self.root.children, pattern):
213 if self.is_swapped(match):
214 repl = "assert_that($act, is_($exp))"
215 act = match["$act"]
216 exp = match["$exp"]
217 repl = repl.replace("$exp", exp).replace("$act", act)
218 self.replace(repl, match.nodes, False, False)
220 def restructure_module(self) -> None:
221 """AI: Move module-level functions into a test class, creating one if none exists."""
222 funs = [stmt for stmt in self.body if stmt.semantic_kind is SemanticKind.FUNCTION]
223 test_classes = [stmt for stmt in self.body if stmt.semantic_kind is SemanticKind.CLASS and stmt.name.startswith("Test")]
224 if len(funs) == 0:
225 return
226 if len(test_classes) == 0:
227 # file does not contain any test class, create a new class and add function in class
228 cls = f"class {self.convert_file_to_test_class()}:\n"
229 for fun in funs:
230 # TODO(FrancescoPezzella): textwrap.indent also indents the lines inside a multi-line string, changing its value.
231 # https://github.com/TNO/Renaissance.Py/issues/283
232 cls += textwrap.indent(convert_function(fun), " ")
233 self.remove([fun])
234 self.insert_before(cls, funs[0])
235 else:
236 # one or more class in file, add function as member of the last class in file
237 for fun in funs:
238 # assuming the class comes first
239 meth = convert_function(fun)
240 self.insert_after(meth, test_classes[-1].body[-1])
241 self.remove(fun)
242 self.commit()
243 for fun in funs:
244 # also change the calling signature of those functions in case they are not test cases
245 function_call = [self.pattern_factory.create_expression(f"{fun.name}($$args)")]
246 for call in match_pattern(self.root.children, function_call):
247 sig = call.nodes[0].signature
248 self.replace(f"self.{sig}", call.nodes, False, False)
249 self.commit()
251 def convert_file_to_test_class(self):
252 """AI: Derive a PascalCase Test-prefixed class name from the file's stem."""
253 path = Path(self.filename)
254 stem = path.stem
255 parts = stem.split("_")
256 if parts[-1].lower() == "test":
257 parts = parts[:-1]
258 name = "".join(word.capitalize() for word in parts)
259 return name if name.startswith("Test") else f"Test{name}"
261 def remove_duplicate_import(self, import_str) -> None:
262 """AI: Remove duplicate occurrences of the given import statement, keeping the first and last."""
263 import_stmt: Sequence[NodeProtocol] = self.pattern_factory.create_statements(import_str) # type: ignore[assignment]
264 # type: ignore[assignment]
265 duplicate_imports = match_pattern(self.body, import_stmt)
267 for match in duplicate_imports[1:-1]:
268 self.remove(match.nodes, False, False)