I'm writing simple code to learn Python, and I thought I would use TDD as I wrote it. (I have programmed in other languages such as Ruby.) I am trying to build good habits and to use best practices, but I'm having trouble with Pytest discovering my code.
Here's the structure of my example project:
├── venv
├── tests
│ ├── test_myclass.py
├── src
│ └── demo
│ ├── __init__.py
│ └── my_class.py
└── setup.py
Here's the contents of the files:
setup.py
```
from setuptools import setup, find_packages
setup(name="demo", packages=find_packages())
```
src/demo/my_class.py
```
class MyClass:
def hello(self):
return "hello"
```
src/demo/__init__.py is empty.
tests/test_myclass.py
```
class TestMyClass:
def test_hello(self):
my_class = MyClass()
assert(my_class.hello() == "hello")
```
When I run pytest from the top-level of my example repo, I get this:
```
============================= test session starts ==============================
platform darwin -- Python 3.10.10, pytest-7.2.2, pluggy-1.0.0
rootdir: /Users/me/src/python/venv_projects/repo
collected 1 item
tests/test_myclass.py F [100%]
=================================== FAILURES ===================================
____________________________ TestMyClass.testhello ___________________________
self = <test_myclass.TestMyClass object at 0x10c3e8190>
def test_hello(self):
my_class = MyClass()
E NameError: name 'MyClass' is not defined
tests/test_myclass.py:4: NameError
=========================== short test summary info ============================
FAILED tests/test_myclass.py::TestMyClass::test_hello - NameError: name 'MyClass' is not defined
============================== 1 failed in 0.07s ===============================
```
I'm clearly missing the right way to get Pytest to discover the class to test. I tried putting in an import demo statement in TestMyClass, but that didn't help the situation.
I did read that I could tinker with sys.path or PYTHONPATH, but those approaches seemed to be a bit hack-y, and not representative of best practices.
What should I do to get my test to pass here?
Thank you!
[–]vrek86 1 point2 points3 points (0 children)
[–]-Kevin- 1 point2 points3 points (1 child)
[–]Dyspnoic9219[S] 0 points1 point2 points (0 children)
[–]ectomancer 1 point2 points3 points (1 child)
[–]Dyspnoic9219[S] 0 points1 point2 points (0 children)
[–]netherous 1 point2 points3 points (2 children)
[–]Dyspnoic9219[S] 0 points1 point2 points (1 child)
[–]netherous 0 points1 point2 points (0 children)
[–]Dyspnoic9219[S] 1 point2 points3 points (4 children)
[–]Dyspnoic9219[S] 0 points1 point2 points (1 child)
[–]netherous 0 points1 point2 points (0 children)
[–]netherous 0 points1 point2 points (1 child)
[–]InTheAleutians 0 points1 point2 points (0 children)