85
85
#
86
86
# ```
87
87
# repository_name
88
- # |-- src
89
- # | `-- package_name
90
- # | |-- __init__.py # optional; required for exporting things under package's namespace
91
- # | |-- python_file.py
92
- # | |-- another_python_file.py
93
- # `-- tests
94
- # |-- fixtures
95
- # | `-- fixture_file.yaml
96
- # `-- test_python_file.py
97
- # |-- LICENSE.md
98
- # |-- CITATION.md
99
- # |-- README.md
100
- # `-- pyproject.toml
88
+ # ├── src
89
+ # │ └── package_name
90
+ # │ ├── __init__.py # optional; required for exporting things under package's namespace
91
+ # │ ├── python_file.py
92
+ # │ └── another_python_file.py
93
+ # ├── tests
94
+ # │ ├── fixtures
95
+ # │ │ └── fixture_file.yaml
96
+ # │ └── test_python_file.py
97
+ # ├── docs
98
+ # │ ├── index.rst
99
+ # │ ├── conf.py
100
+ # │ └── ...
101
+ # ├── LICENSE.md
102
+ # ├── CITATION.md
103
+ # ├── README.md
104
+ # └── pyproject.toml
101
105
# ```
102
106
#
103
107
#
@@ -546,20 +550,14 @@ def process():
546
550
# %%
547
551
# %%writefile greetings_repo/tests/test_greeter.py
548
552
549
- import os
553
+ from pathlib import Path
550
554
551
555
import yaml
552
556
553
557
from greetings .greeter import greet
554
558
555
559
def test_greet ():
556
- with open (
557
- os .path .join (
558
- os .path .dirname (__file__ ),
559
- 'fixtures' ,
560
- 'samples.yaml'
561
- )
562
- ) as fixtures_file :
560
+ with open (Path (__file__ ).parent / 'fixtures' / 'samples.yaml' ) as fixtures_file :
563
561
fixtures = yaml .safe_load (fixtures_file )
564
562
for fixture in fixtures :
565
563
answer = fixture .pop ('answer' )
@@ -607,21 +605,15 @@ def test_greet():
607
605
# %%
608
606
# %%writefile greetings_repo/tests/test_greeter.py
609
607
610
- import os
608
+ from pathlib import Path
611
609
612
610
import pytest
613
611
import yaml
614
612
615
613
from greetings .greeter import greet
616
614
617
615
def read_fixture ():
618
- with open (
619
- os .path .join (
620
- os .path .dirname (__file__ ),
621
- 'fixtures' ,
622
- 'samples.yaml'
623
- )
624
- ) as fixtures_file :
616
+ with open (Path (__file__ ).parent / 'fixtures' / 'samples.yaml' ) as fixtures_file :
625
617
fixtures = yaml .safe_load (fixtures_file )
626
618
return fixtures
627
619
@@ -647,6 +639,15 @@ def test_greeter(fixture):
647
639
# cd greetings_repo
648
640
# pytest --doctest-modules
649
641
642
+ # %% [markdown]
643
+ # The `ImportError` appears here `pytest` requires `__init__.py` files to discover test cases when using relative imports (though the library works fine without it).
644
+
645
+ # %% magic_args="--no-raise-error" language="bash"
646
+ #
647
+ # cd greetings_repo
648
+ # touch src/greetings/__init__.py
649
+ # pytest --doctest-modules
650
+
650
651
# %% [markdown]
651
652
# Finally, we typically don't want to include the tests when we distribute our software for our users.
652
653
# We can also add pytest as an "optional" dependency for the developers of our package.
0 commit comments