Skip to content

Commit 6f1b5cd

Browse files
authored
uvx --python=3.12 unittest2pytest --write test_factorial.py
1 parent 5e0e9ad commit 6f1b5cd

1 file changed

Lines changed: 29 additions & 15 deletions

File tree

maths/test_factorial.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
import unittest
2-
from factorial import factorial
1+
# /// script
2+
# requires-python = ">=3.13"
3+
# dependencies = [
4+
# "pytest",
5+
# ]
6+
# ///
37

8+
import pytest
49

5-
class TestFactorial(unittest.TestCase):
6-
def test_zero(self):
7-
self.assertEqual(factorial(0), 1)
10+
from maths.factorial import factorial, factorial_recursive
811

9-
def test_positive_integers(self):
10-
self.assertEqual(factorial(1), 1)
11-
self.assertEqual(factorial(5), 120)
12-
self.assertEqual(factorial(7), 5040)
1312

14-
def test_large_number(self):
15-
self.assertEqual(factorial(10), 3628800)
13+
@pytest.mark.parametrize("function", [factorial, factorial_recursive])
14+
def test_zero(function):
15+
assert function(0) == 1
1616

17-
def test_negative_number(self):
18-
with self.assertRaises(ValueError):
19-
factorial(-3)
17+
18+
@pytest.mark.parametrize("function", [factorial, factorial_recursive])
19+
def test_positive_integers(function):
20+
assert function(1) == 1
21+
assert function(5) == 120
22+
assert function(7) == 5040
23+
24+
25+
@pytest.mark.parametrize("function", [factorial, factorial_recursive])
26+
def test_large_number(function):
27+
assert function(10) == 3628800
28+
29+
30+
@pytest.mark.parametrize("function", [factorial, factorial_recursive])
31+
def test_negative_number(function):
32+
with pytest.raises(ValueError):
33+
function(-3)
2034

2135

2236
if __name__ == "__main__":
23-
unittest.main()
37+
pytest.main(["-v", __file__])

0 commit comments

Comments
 (0)