Skip to content

Commit 5d0ab48

Browse files
Merge pull request #31 from kiddopro/mocking_inputs
Mocking inputs
2 parents 6be6019 + e1b97bd commit 5d0ab48

File tree

35 files changed

+398
-171
lines changed

35 files changed

+398
-171
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Sum all three input numbers and print on the console the result
2-
first_number = input("First input")
3-
second_number = input("Second input")
4-
third_number = input("Third input")
2+
first_number = int(input("First input"))
3+
second_number = int(input("Second input"))
4+
third_number = int(input("Third input"))
55
# print here the sum of three inputs
66

77
print(first_number+second_number)

exercises/002-sum_of_three_numbers/test.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@ def test_use_print():
88
regex = re.compile(r"print(\s)*\(")
99
assert bool(regex.search(content)) == True
1010

11-
@pytest.mark.it('Almost there! But you need to convert the incoming input from string to integer')
12-
def test_convert_inputs(capsys, app):
11+
@pytest.mark.it('The solution should return the expected output. Tested with 2, 3, 6')
12+
def test_sum_expected_inputs(capsys, app):
1313

14-
fake_input = ["2","3","4"] #fake input
14+
fake_input = ["2","3","6"] #fake input
1515
with mock.patch('builtins.input', lambda x: fake_input.pop()):
1616
app()
1717
captured = capsys.readouterr()
18-
assert captured.out != "432\n"
18+
assert captured.out == "11\n"
1919

20-
@pytest.mark.it('Sum all three input numbers and print on the console the result')
21-
def test_add_variables(capsys, app):
20+
@pytest.mark.it('The solution must sum all entries. Tested with 0, 3, 7')
21+
def test_sum_another_inputs(capsys, app):
2222

23-
fake_input = ["2","3","4"] #fake input
23+
fake_input = ["0","3","7"] #fake input
2424
with mock.patch('builtins.input', lambda x: fake_input.pop()):
2525
app()
2626
captured = capsys.readouterr()
27-
assert captured.out == "9\n"
27+
assert captured.out == "10\n"
28+
29+
@pytest.mark.it('The solution must sum all entries. Tested with 0, 0, 0')
30+
def test_sum_with_zero(capsys, app):
31+
32+
fake_input = ["0","0","0"] #fake input
33+
with mock.patch('builtins.input', lambda x: fake_input.pop()):
34+
app()
35+
captured = capsys.readouterr()
36+
assert captured.out == "0\n"
2837

2938

3039

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#Complete the function to return the area of the triangle.
2-
b = input("Size of B")
3-
h = input("Size of H")
4-
52
def area_of_triangle(arg1, arg2):
63
#your code here, please remove the "None"
74
return None
85

96
# Testing your function
10-
print(area_of_triangle(b, h))
7+
print(area_of_triangle(3, 5))
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import io, sys, os, pytest, json, mock
22
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
33

4-
@pytest.mark.it('Calculate the area of the triangle.')
5-
def test_area_of_triangle(stdin):
64

7-
_stdin = json.loads(stdin)
8-
with mock.patch('builtins.input', lambda x: _stdin.pop(0)):
9-
10-
sys.stdout = buffer = io.StringIO()
5+
@pytest.mark.it('The function area_of_triangle should exist')
6+
def test_function_exists(capsys):
7+
try:
118
import app
12-
_stdin = json.loads(stdin)
13-
result = int(_stdin[0]) * int(_stdin[1]) / 2
14-
assert buffer.getvalue() == str(result) + "\n"
15-
9+
app.area_of_triangle
10+
except AttributeError:
11+
raise AttributeError("The function 'area_of_triangle' should exist on app.py")
1612

13+
@pytest.mark.it('The solution should return the expected output. Testing with 3 and 5')
14+
def test_convert_inputs(capsys, app):
15+
result = app.area_of_triangle(3, 5)
16+
assert result == 7.5
1717

18+
@pytest.mark.it('The solution should return the expected output. Testing with 4 and 6')
19+
def test_convert_inputs(capsys, app):
20+
result = app.area_of_triangle(4, 6)
21+
assert result == 12

exercises/004-hello_harry/app.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@ def hello_name(name):
33

44
return None
55

6-
7-
86
#Invoke the function with your name as the function's argument.
97
print(hello_name("Bob"))
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
n=int(raw_input())
2-
d=dict()
3-
for i in range(1,n+1):
4-
d[i]=i*i
1+
def generate_dict(n):
2+
d=dict()
3+
for i in range(1,n+1):
4+
d[i]=i*i
5+
return d
56

6-
print d
7+
print(generate_dict(n))

exercises/22-Integral/test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest,os,re,io,sys, mock, json
2+
3+
@pytest.mark.it('The function generate_dict should exist')
4+
def test_function_existence(capsys, app):
5+
assert app.generate_dict
6+
7+
@pytest.mark.it('The function should return a dictionary')
8+
def test_typeof_return(capsys, app):
9+
assert type(app.generate_dict(7)) == type({})
10+
11+
@pytest.mark.it('The function should return the expected output')
12+
def test_expected_output(capsys, app):
13+
assert app.generate_dict(8) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
14+
15+
@pytest.mark.it('The function should work with other entries')
16+
def test_another_entry_5(capsys, app):
17+
assert app.generate_dict(5) == {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
18+
19+
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
values=raw_input()
1+
values=input("")
22
l=values.split(",")
33
t=tuple(l)
4-
print l
5-
print t
4+
print (l)
5+
print (t)
6+
7+
#
8+
values=input("")
9+
t=tuple(values.split(','))
10+
print(t)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import io, sys, os, pytest, json, mock
2+
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
3+
4+
5+
@pytest.mark.it('The output should be the expected for this solution')
6+
def test_output(capsys, app):
7+
fake_input = ["34,67,55,33,12,98"] #fake input
8+
with mock.patch('builtins.input', lambda x: fake_input.pop()):
9+
app()
10+
captured = capsys.readouterr()
11+
assert captured.out == "['34', '67', '55', '33', '12', '98']\n" or "('34', '67', '55', '33', '12', '98')\n"
12+
13+
@pytest.mark.it('The solution should work with other parameters')
14+
def test_output_2(capsys, app):
15+
fake_input = ["11,56,23,12,02"] #fake input
16+
with mock.patch('builtins.input', lambda x: fake_input.pop()):
17+
app()
18+
captured = capsys.readouterr()
19+
assert captured.out == "['11', '56', '23', '12', '02']\n" or "('11', '56', '23', '12', '02')\n"
20+
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
#!/usr/bin/env python
21
import math
3-
c=50
4-
h=30
5-
value = []
6-
items=[x for x in raw_input().split(',')]
7-
for d in items:
8-
value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))
2+
def print_formula(num):
3+
return round(math.sqrt(2*50*num/30))
94

10-
print ','.join(value)
5+
print(print_formula(5))

0 commit comments

Comments
 (0)