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)

exercises/23-list-and-tuple/test.py

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))

exercises/25-print-formula/test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest, io, sys, json, mock, re, os
2+
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
3+
4+
@pytest.mark.it('The function print_formula must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.print_formula
7+
8+
@pytest.mark.it('The solution should work as expected')
9+
def test_expected_output(capsys, app):
10+
assert app.print_formula(100,150,180) == "18,22,24"
11+
12+
@pytest.mark.it('The solution should work as expected')
13+
def test_another_output(capsys, app):
14+
assert app.print_formula(200,90,300) == "26,17,32"
15+
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
input_str = raw_input()
2-
dimensions=[int(x) for x in input_str.split(',')]
3-
rowNum=dimensions[0]
4-
colNum=dimensions[1]
5-
multilist = [[0 for col in range(colNum)] for row in range(rowNum)]
1+
def two_dimensional_array(nList, nElements):
2+
dimensions=[int(x) for x in "{},{}".format(nList,nElements).split(',')]
3+
rowNum=dimensions[0]
4+
colNum=dimensions[1]
5+
multilist = [[0 for col in range(colNum)] for row in range(rowNum)]
66

7-
for row in range(rowNum):
8-
for col in range(colNum):
9-
multilist[row][col]= row*col
7+
for row in range(rowNum):
8+
for col in range(colNum):
9+
multilist[row][col]= row*col
1010

11-
print multilist
11+
return (multilist)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import pytest,os,re,io,sys, mock, json
3+
4+
@pytest.mark.it('The function two_dimensional_array must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.two_dimensional_array
7+
8+
@pytest.mark.it('The function should return the expected output.')
9+
def test_expected_output(capsys, app):
10+
assert app.two_dimensional_array(3,5) == [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
11+
12+
@pytest.mark.it('The function should work with other entries. Testing with 2,7')
13+
def test_expected_output(capsys, app):
14+
assert app.two_dimensional_array(2,7) == [[0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5, 6]]
15+
16+
@pytest.mark.it('The function should work with other entries. Testing with 2,7')
17+
def test_expected_output(capsys, app):
18+
assert app.two_dimensional_array(1,10) == [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
items=[x for x in raw_input().split(',')]
2-
items.sort()
3-
print ','.join(items)
1+
def sequence_of_words(words):
2+
items=[x for x in "{}".format(words).split(',')]
3+
items.sort()
4+
return (','.join(items))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest, io, sys, json, mock, re, os
2+
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
3+
4+
@pytest.mark.it('The function sequence_of_words must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.sequence_of_words
7+
8+
@pytest.mark.it('The function should return the expected output')
9+
def test_function_existence(capsys, app):
10+
assert app.sequence_of_words("bag,hello,without,world") == "bag,hello,without,world"
11+
12+
@pytest.mark.it('The function should return the expected output')
13+
def test_function_existence(capsys, app):
14+
assert app.sequence_of_words("No,pain,gain") == "No,gain,pain"
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
lines = []
2-
while True:
3-
s = raw_input()
4-
if s:
5-
lines.append(s.upper())
6-
else:
7-
break;
1+
def lines(text):
2+
return text.upper()
83

9-
for sentence in lines:
10-
print sentence
4+
print(lines('Hello world'))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest, io, sys, json, mock, re, os
2+
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
3+
4+
@pytest.mark.it('The function lines must exist')
5+
def test_function_existence(capsys, app):
6+
assert app.lines
7+
8+
@pytest.mark.it('The function should return the expected output')
9+
def test_expected_output(capsys, app):
10+
assert app.lines("hello world") == "HELLO WORLD"
11+
12+
@pytest.mark.it('The function should return the expected output')
13+
def test_another_output(capsys, app):
14+
assert app.lines("LeT the WOrld know YoU") == "LET THE WORLD KNOW YOU"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
s = input()
2-
words = [word for word in s.split(" ")]
3-
print (" ".join(sorted(list(set(words)))))
1+
def remove_duplicate_words(text):
2+
words = text.split()
3+
return (" ".join(sorted(list(set(words)))))
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import pytest, io, sys, json, mock, re, os
22

3-
@pytest.mark.it('Your solution should work as expected')
3+
@pytest.mark.it('The function remove_duplicate_words must exist')
4+
def test_function_existence(capsys, app):
5+
app.remove_duplicate_words
6+
7+
@pytest.mark.it('The function should return the expected output')
48
def test_expected_output(capsys, app):
5-
fake_input=['Hola como Hola']
6-
with mock.patch('builtins.input', lambda x: fake_input.pop()):
7-
app()
8-
captured = capsys.readouterr()
9-
assert captured.out == " como \n"
9+
assert app.remove_duplicate_words("hello world and practice makes perfect and hello world again") == "again and hello makes perfect practice world"
10+
11+
@pytest.mark.it('The function should work with other entries')
12+
def test_expected_output_2(capsys, app):
13+
assert app.remove_duplicate_words("lets try this again with another try") == "again another lets this try with"
14+
15+
@pytest.mark.it('The function should work with other entries')
16+
def test_expected_output_3(capsys, app):
17+
assert app.remove_duplicate_words("Jacke was Named Jacke by his mother") == "Jacke Named by his mother was"
18+
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
value = []
2-
items=[x for x in raw_input().split(',')]
3-
for p in items:
4-
intp = int(p, 2)
5-
if not intp%5:
6-
value.append(p)
1+
def divisable_binary(text):
2+
value=[]
3+
items=[x for x in text.split(',')]
4+
for p in items:
5+
intp = int(p, 2)
6+
if not intp%5:
7+
value.append(p)
78

8-
print ','.join(value)
9+
return (','.join(value))

exercises/30-divisable-binary/test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest, io, sys, json, mock, re, os
2+
3+
@pytest.mark.it('The function divisable_binary must exist')
4+
def test_function_existence(capsys, app):
5+
assert app.divisable_binary
6+
7+
@pytest.mark.it('The function should return the expected output')
8+
def test_expected_output(capsys, app):
9+
assert app.divisable_binary("0100,0011,1010,1001") == "1010"
10+
11+
@pytest.mark.it('The function should work with other parameters. testing with 1111,1000,0101,0000')
12+
def test_expected_output_2(capsys, app):
13+
assert app.divisable_binary("1111,1000,0101,0000") == "1111,0101,0000"
14+
15+
@pytest.mark.it("The function should work with other parameters. Testing with 1000")
16+
def test_expected_output_3(capsys, app):
17+
assert app.divisable_binary("1000,1000,1000,1000") == ""

0 commit comments

Comments
 (0)