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'
5
3
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" )
24
5
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
28
10
29
11
@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" )
34
17
35
18
@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
40
28
41
29
@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
0 commit comments