1
+ import pytest
2
+
3
+ from calculator import simple_calculator
4
+
5
+
6
+ @pytest .mark .parametrize ("arg, expected" , [
7
+ ('2 + 3' , 5 ),
8
+ ('5 + 11' , 16 ),
9
+ ('12 + 18' , 30 ),
10
+ ])
11
+ def test_sum (arg , expected ):
12
+ assert simple_calculator (arg ) == expected
13
+
14
+
15
+ @pytest .mark .parametrize ("arg, expected" , [
16
+ ('3 - 2' , 1 ),
17
+ ('16 - 11' , 5 ),
18
+ ('12 - 18' , - 6 ),
19
+ ])
20
+ def test_subtract (arg , expected ):
21
+ assert simple_calculator (arg ) == expected
22
+
23
+
24
+ @pytest .mark .parametrize ("arg, expected" , [
25
+ ('2 * 3' , 6 ),
26
+ ('-5 * -11' , 55 ),
27
+ ('3 * -6' , - 18 ),
28
+ ])
29
+ def test_multiply (arg , expected ):
30
+ assert simple_calculator (arg ) == expected
31
+
32
+
33
+ @pytest .mark .parametrize ("arg, expected" , [
34
+ ('2 / 3' , 0.67 ),
35
+ ('1 / 5' , 0.2 ),
36
+ ('-2 / 175' , - 0.01 ),
37
+ ])
38
+ def test_true_division (arg , expected ):
39
+ assert round (simple_calculator (arg ), 2 ) == expected
40
+
41
+
42
+ @pytest .mark .parametrize ("arg" , [
43
+ 'a / 3' , '2 / b' , 'c / d' , '1 2 3' , '1 ^ 2' ,
44
+ '1 x 2' , 'some random string' , '1 / 0' ,
45
+ 'really_bad_data'
46
+ ])
47
+ def test_bad_inputs (arg ):
48
+ with pytest .raises (ValueError ):
49
+ simple_calculator (arg )
0 commit comments