Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c39328a

Browse files
Merge pull request #99 from AnggieAlava/bug/1539
Print more flexible
2 parents 9bc2b15 + 49218f6 commit c39328a

File tree

1 file changed

+17
-9
lines changed
  • exercises/11-Create-A-New-Function

1 file changed

+17
-9
lines changed

‎exercises/11-Create-A-New-Function/test.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,42 @@
77
import mock
88
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
99

10+
1011
@pytest.mark.it('The function generate_random should exist')
1112
def test_function_exists():
1213
try:
1314
from app import generate_random
1415
except ImportError:
15-
raise ImportError("The function 'generate_random' should exist on app.py")
16+
raise ImportError(
17+
"The function 'generate_random' should exist on app.py")
18+
1619

1720
@pytest.mark.it("The function 'generate_random' should return a random number between 0 and 9")
1821
def test_for_return():
1922
from app import generate_random
2023
result = generate_random()
2124
assert result is not None
22-
for x in range(0,20):
25+
for x in range(0,20):
2326
result = generate_random()
24-
assert result <= 9 and result >= 0
27+
assert result <= 9 and result >= 0
28+
2529

2630
@pytest.mark.it('Use the function randinit() or randrange()')
2731
def test_for_type_random():
2832
with open(path, 'r') as content_file:
2933
content = content_file.read()
3034
regex = re.compile(r"random.randint\s*\(")
3135
regex2 = re.compile(r"random.randrange\s*\(")
32-
assert bool(regex.search(content)) == True or bool(regex2.search(content)) == True
36+
assert bool(regex.search(content)) == True or bool(
37+
regex2.search(content)) == True
38+
3339

3440
@pytest.mark.it('You should print() the output of the function')
3541
def test_function_called_for():
36-
37-
with open(path, 'r') as content_file:
38-
content = content_file.read()
39-
regex = re.compile(r"print\s*\(\s*generate_random\s*\(\s*\)\s*\)")
40-
assert bool(regex.search(content)) == True
42+
captured_output = io.StringIO()
43+
sys.stdout = captured_output
44+
app.generate_random()
45+
sys.stdout = sys.__stdout__
46+
output = captured_output.getvalue()
47+
regex = re.compile(r"\d{0,9}")
48+
assert bool(regex.search(output)) == True

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /