Skip to content

Commit 791b6b4

Browse files
committed
updated tests for first 5 exercises
1 parent de3c179 commit 791b6b4

File tree

13 files changed

+106
-151
lines changed

13 files changed

+106
-151
lines changed

exercises/01-hello-world/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# `01` Hello World
22

33
In Python, we use `print` to make the computer write anything we want (the content of a variable, a given string, etc.)
4-
in something called `"the console".`
4+
in something called `the console`.
55

66
Every language has a console, as it was the only way to interact with the users at the beginning
77
(before the Windows or MacOS arrived). Today, printing in the console is used mostly as a
@@ -15,6 +15,7 @@ print("How are you?")
1515
📝 Instructions:
1616

1717
```md
18-
Use the `print()` function to print `"Hello World"` on the console. Feel free to try other things as well.
18+
Use the `print()` function to print `Hello World` on the console.
1919
```
20+
Feel free to try other things as well.
2021

exercises/01-hello-world/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# On the next line, use Python's print function to say `Hello World` in the console (this exercise is case-sensitive!)
1+
# On the next line, use Python's print function to say `Hello World` in the console (this exercise is case-sensitive!)

exercises/01-hello-world/test.py

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
1-
import io
2-
import sys
3-
import os
4-
sys.stdout = buffer = io.StringIO()
5-
6-
# from app import my_function
1+
import io, sys, os
72
import pytest
8-
import app
9-
10-
# @pytest.mark.it('Your code needs to print hello on the console')
11-
# def test_for_file_output(capsys):
12-
# captured = buffer.getvalue()
13-
# assert captured == "hello\n" #add \n because the console jumps the line on every print
14-
15-
# @pytest.mark.it('Your function needs to print "Hello Inside Function" on the console')
16-
# def test_for_function_output(capsys):
17-
# my_function()
18-
# captured = capsys.readouterr()
19-
# assert captured.out == "Hello Inside Function\n"
203

21-
# @pytest.mark.it('Your function needs to return True')
22-
# def test_for_function_return(capsys):
23-
# assert my_function() == True
24-
25-
@pytest.mark.it("Output 'Hello World'")
26-
def test_output():
27-
captured = buffer.getvalue()
28-
assert "Hello World\n" in captured
4+
@pytest.mark.it("Output 'Hello World' case sensitive")
5+
def test_output(capsys, app):
6+
app()
7+
captured = capsys.readouterr()
8+
assert "Hello World\n" == captured.out
299
# convert everything in the buffer to lower case, captured to lower case
3010

31-
3211
@pytest.mark.it("Use print function")
3312
def test_print():
3413
f = open(os.path.dirname(os.path.abspath(__file__)) + '/app.py')
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
my_list = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday']
33

4-
#1. print the item here
4+
# 1. print the item here
55

6-
#2. change 'thursday'a value here to None
6+
# 2. change the position were 'thursday' is to None
77

8-
#3. print the position of step 2
8+
# 3. print that position now here
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import io
2-
import sys
3-
sys.stdout = buffer = io.StringIO()
4-
5-
from app import my_list
6-
import pytest
1+
import io, sys, pytest
72

83
@pytest.mark.it('Your console have to print the 3rd item from the `list`')
9-
def test_output_one():
10-
print(my_list[2])
11-
captured = buffer.getvalue()
12-
assert "tuesday\n" in captured
4+
def test_output_one(capsys, app):
5+
app()
6+
captured = capsys.readouterr()
7+
assert "tuesday\n" in captured.out
138

149
@pytest.mark.it('Your code have to print the position of step 2')
15-
def test_output_two():
16-
print(my_list[2])
17-
captured = buffer.getvalue()
18-
assert "None\n" in captured
10+
def test_output_two(capsys, app):
11+
app()
12+
captured = capsys.readouterr()
13+
assert "None\n" in captured.out
1914

20-
@pytest.mark.it('Set index[4] to None')
15+
@pytest.mark.it('Set the position were thrusday is to None')
2116
def test_position_two():
17+
from app import my_list
2218
assert my_list[4] is None

exercises/01.2-Retrieve-items/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
my_list = [4,5,734,43,45,100,4,56,23,67,23,58,45,3,100,4,56,23]
22

3-
#Print in the console the 1st and 4th element from the list:
3+
# Print in the console the 1st element on the list
4+
5+
# Print in the console the 4th element on the list

exercises/01.2-Retrieve-items/test.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import io
2-
import sys
3-
sys.stdout = buffer = io.StringIO()
4-
5-
from app import my_list
6-
import pytest
1+
import io, sys, pytest
72

83

94
@pytest.mark.it('You have to print the 1st element of the list')
10-
def test_output_one():
11-
print(my_list[0])
12-
captured = buffer.getvalue()
13-
assert "4\n" in captured
5+
def test_output_one(capsys, app):
6+
app()
7+
captured = capsys.readouterr()
8+
assert "4\n" in captured.out
149

1510
@pytest.mark.it('You have to print the 4th element of the list')
16-
def test_output_fourd():
17-
print(my_list[3])
18-
captured = buffer.getvalue()
19-
assert "43\n" in captured
11+
def test_output_fourd(capsys, app):
12+
app()
13+
captured = capsys.readouterr()
14+
assert "43\n" in captured.out
2015

exercises/01.3-Print-the-last-one/app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# import the random package here "import random"
2+
import random
23

34
def generate_random_list():
45
aux_list = []
@@ -10,4 +11,4 @@ def generate_random_list():
1011
return aux_list
1112
my_stupid_list = generate_random_list()
1213

13-
# Feel happy to write the code below this comment, good luck!:
14+
# Feel happy to write the code below this comment, good luck!:
Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,33 @@
1-
import io
2-
import sys
3-
import app
4-
import pytest
1+
import io, sys, pytest, os, re
2+
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
53

6-
import os
7-
# from app import the_last_one
8-
import app
9-
10-
11-
sys.stdout = buffer = io.StringIO()
12-
13-
# @pytest.mark.it("create and assign the value to the variable the_last_one")
14-
# def test_create_assign():
15-
# assert app.the_last_one is not None
16-
17-
18-
# @pytest.mark.it("print in the console the_last_one")
19-
# def test_output():
20-
# captured = buffer.getvalue()
21-
# assert str(app.the_last_one) in captured
22-
23-
@pytest.mark.it("Import random function")
4+
@pytest.mark.it("Import the random package")
245
def test_import_random():
25-
f = open(os.path.dirname(os.path.abspath(__file__)) + '/app.py')
26-
content = f.read()
27-
assert content.find("import random") > 0
6+
with open(path, 'r') as content_file:
7+
content = content_file.read()
8+
regex = re.compile(r"import(\s)+random")
9+
assert bool(regex.search(content)) == True
2810

2911
@pytest.mark.it("Create the variable the_last_one")
30-
def test_create():
31-
f = open(os.path.dirname(os.path.abspath(__file__)) + '/app.py')
32-
content = f.read()
33-
assert content.find("the_last_one") > 0
12+
def test_variable_exists(app):
13+
try:
14+
app.the_last_one
15+
except AttributeError:
16+
raise AttributeError("The variable 'the_last_one' should exist on app.py")
3417

3518
@pytest.mark.it("Assign the last number to the variable")
36-
def test_assing():
37-
f = open(os.path.dirname(os.path.abspath(__file__)) + '/app.py')
38-
content = f.read()
39-
assert content.find("the_last_one = my_stupid_list[-1]") > 0
19+
def test_assing(app):
20+
assert app.the_last_one == app.my_stupid_list[-1]
21+
22+
@pytest.mark.it("Use the function print")
23+
def test_use_print():
24+
with open(path, 'r') as content_file:
25+
content = content_file.read()
26+
regex = re.compile(r"print(\s)*\(")
27+
assert bool(regex.search(content)) == True
4028

4129
@pytest.mark.it("Print the last element")
42-
def test_output():
43-
f = open(os.path.dirname(os.path.abspath(__file__)) + '/app.py')
44-
content = f.read()
45-
assert content.find("print(the_last_one)") > 0
30+
def test_output(capsys, app):
31+
import app
32+
captured = capsys.readouterr()
33+
assert str(app.my_stupid_list[-1])+"\n" in captured.out

exercises/01.4-Add-item-to-list/test.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
1-
import io
2-
import sys
3-
sys.stdout = buffer = io.StringIO()
1+
import io, sys, pytest, os, random
42

5-
import pytest
6-
import app
7-
import os
8-
import random
9-
from app import my_list
10-
11-
# @pytest.mark.it("Add ten random numbers to the list")
12-
# def test_add_numb():
13-
# assert app.my_list.append(random.randint(1, 100)) is not None
14-
15-
# @pytest.mark.it("Output of the list with 15 items numbers")
16-
# def test_output():
17-
# captured = buffer.getvalue()
18-
# assert str(app.my_list) in captured
19-
20-
@pytest.mark.it("Import random function")
3+
@pytest.mark.it("Import the random package")
214
def test_import_random():
22-
f = open(os.path.dirname(os.path.abspath(__file__)) + '/app.py')
23-
content = f.read()
24-
assert content.find("import random") > 0
5+
with open(path, 'r') as content_file:
6+
content = content_file.read()
7+
regex = re.compile(r"import(\s)+random")
8+
assert bool(regex.search(content)) == True
259

2610
@pytest.mark.it("Use the for loop")
27-
def test_for():
28-
f = open(os.path.dirname(os.path.abspath(__file__)) + '/app.py')
29-
content = f.read()
30-
assert content.find("for x in range(1, 10):") > 0
11+
def test_for_loop():
12+
with open(path, 'r') as content_file:
13+
content = content_file.read()
14+
regex = re.compile(r"for(\s)+(\w)+in(\s)+range")
15+
assert bool(regex.search(content)) == True
3116

3217
@pytest.mark.it("Add the ten random numbers to the list")
3318
def test_add():

0 commit comments

Comments
 (0)