Skip to content

Commit c5e949a

Browse files
committed
add tests from demo
1 parent a535ef2 commit c5e949a

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/python_package/mockup.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,33 @@ def hello_world(n: int) -> str:
1313
-------
1414
str
1515
str of 'hello world' n-times
16+
17+
Examples
18+
--------
19+
>>> hello_world(3)
20+
'hello world hello world hello world'
1621
"""
1722
return " ".join(repeat("hello world", n))
23+
24+
25+
def saved_world(filename: str) -> int:
26+
"""
27+
Count how many times 'hello world' is in a file.
28+
29+
Parameters
30+
----------
31+
filename : str
32+
The file to read
33+
34+
Returns
35+
-------
36+
int
37+
How many times 'hello world' is in the file
38+
39+
Examples
40+
--------
41+
>>> saved_world("not-real.txt") # doctest: +SKIP
42+
"""
43+
with open(filename, "r") as f:
44+
content = f.read()
45+
return content.count("hello world")

tests/test_mockup.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1-
from python_package import hello_world
2-
1+
from python_package import hello_world, saved_world
2+
import pytest
33

44
def test_hello_world_3times():
55
expected = "hello world hello world hello world"
66
result = hello_world(3)
77
assert result == expected
8+
9+
10+
@pytest.mark.xfail(raises=TypeError)
11+
def test_hello_world_str():
12+
hello_world("3")
13+
14+
15+
@pytest.fixture
16+
def temp_file():
17+
# set up
18+
filename = "temp_hellooo.txt"
19+
with open(filename, "w") as f:
20+
f.write("hello world hello world hello world")
21+
yield filename
22+
# clean up
23+
import os
24+
os.remove(filename)
25+
26+
27+
def test_saved_world_3times(temp_file):
28+
result = saved_world(temp_file)
29+
assert result == 3

0 commit comments

Comments
 (0)