python中找不到模块_python – pytest找不到模块

参见英文答案 > Ensuring py.test includes the application directory in sys.path????????????????????????????????????3个

我跟随pytest good practices或者至少我认为我是.但是,pytest无法找到我的模块.似乎不在PYTHONPATH中包含当前目录.

源文件:

def add(x, y):

return x + y

测试文件:

import pytest

from junk.ook import add

def test_add_true():

assert add(1, 1) == 2

而shell输出的Python 3虚拟环境称为“p3”.

p3; pwd

/home/usr/tmp/junk

p3; ls

total 0

0 junk/ 0 tests/

p3; ls junk

total 4.0K

4.0K ook.py 0 __init__.py

p3; ls tests

total 4.0K

4.0K test_ook.py 0 __pycache__/

p3; pytest

============================= test session starts ==============================

platform linux -- Python 3.4.5, pytest-3.4.1, py-1.5.2, pluggy-0.6.0

rootdir: /home/usr/tmp/junk, inifile:

collected 0 items / 1 errors

==================================== ERRORS ====================================

______________________ ERROR collecting tests/test_ook.py ______________________

ImportError while importing test module '/home/usr/tmp/junk/tests/test_ook.py'.

Hint: make sure your test modules/packages have valid Python names.

Traceback:

tests/test_ook.py:2: in

from junk.ook import add

E ImportError: No module named 'junk'

!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!

=========================== 1 error in 0.08 seconds ============================

def test_add_true():

assert add(1, 1) == 2

但是,运行以下操作确实可以正常工作.

p3; python -m pytest tests/

============================= test session starts ==============================

platform linux -- Python 3.4.5, pytest-3.4.1, py-1.5.2, pluggy-0.6.0

rootdir: /home/usr/tmp/junk, inifile:

collected 1 item

tests/test_ook.py . [100%]

=========================== 1 passed in 0.02 seconds ===========================

我究竟做错了什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值