Skip to content

Instantly share code, notes, and snippets.

@nicoddemus
Last active April 15, 2022 19:23
Show Gist options
  • Save nicoddemus/6abbc43236dd042fd053 to your computer and use it in GitHub Desktop.
Save nicoddemus/6abbc43236dd042fd053 to your computer and use it in GitHub Desktop.
Example on how to structure a package and testing modules

Arrange files like this:

apple/
  __init__.py
  apple.py
tests/
  test_everything.py

And execute with:

export PYTHONPATH=.
py.test

(This Gist was used to answer a question at stackoverflow)

def eat():
return 'eaten'
from apple import apple
def test_apple():
assert apple.eat() == 'eaten'
@chrisamow
Copy link

chrisamow commented Mar 2, 2017

I thought I was going crazy with py.test problems, so I tried your example... (something going on with python3.6 ?!)

➜  apple ★ tree                                                                        [0] 23:49:16
.
├── apple.py
├── __init__.py
└── tests
    └── test_everything.py

1 directory, 3 files
➜  apple ★ cat apple.py                                                                [0] 23:49:18
def eat():
    return 'eaten'

➜  apple ★ cat tests/test_everything.py                                                [0] 23:49:34
from apple import apple

def test_apple():
    assert apple.eat() == 'eaten'
➜  apple ★ export PYTHONPATH=.                                                         [0] 23:49:43
➜  apple ★ echo $PYTHONPATH                                                            [0] 23:50:05
.
➜  apple ★ py.test                                                                     [0] 23:50:17
======================================= test session starts ========================================
platform linux -- Python 3.6.0, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /home/chris/work/apple, inifile: 
plugins: pudb-0.5
collected 0 items / 1 errors 

============================================== ERRORS ==============================================
____________________________ ERROR collecting tests/test_everything.py _____________________________
ImportError while importing test module '/home/chris/work/apple/tests/test_everything.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../anaconda3/lib/python3.6/site-packages/pytest_pudb.py:26: in pudb_b_import
    return old_import(name, *args, **kwargs)
tests/test_everything.py:1: in <module>
    from apple import apple
E   ImportError: cannot import name 'apple'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================== 1 error in 0.15 seconds ======================================
➜  apple ★ which python                                                                [0] 23:50:41
/home/chris/anaconda3/bin/python
➜  apple ★ which py.test                                                               [0] 23:50:47
/home/chris/anaconda3/bin/py.test
➜  apple ★                                                                             [0] 23:50:51
➜  apple ★ python --version                                                            [0] 23:51:47
Python 3.6.0 :: Anaconda 4.3.0 (64-bit)

@valeriikundas
Copy link

@chrisamow I know, it's been a long long time. But did you solve that problem?

@xealits
Copy link

xealits commented Dec 25, 2019

Wanted to notice that you can run set an environment variable just for one command:

PYTHONPATH=. pytest

# or in fish shell
env PYTHONPATH=. pytest

It worked for me. Thanks for your example on SO!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment