V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yujianwjj
V2EX  ›  Python

请教:如何运行 Python 单元测试文件

  •  
  •   yujianwjj · 2017-08-14 14:25:13 +08:00 · 2327 次点击
    这是一个创建于 2418 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目结构如下:

    ├── app
    │   ├── __init__.py
    │   └── utils.py
    ├── tests
    │   ├── test_app.py
    

    app/utils的内容如下:

    class A:
    	pass
    

    tests/test_app内容如下:

    from app.utils import A
    def test_app():
    	pass
    

    直接运行py.test tests/test_app.py,会报找不到 app 这个模块,请问需要怎么修改,才能运行 tests 下面的测试。

    7 条回复    2017-08-14 19:25:34 +08:00
    pekingzcc
        1
    pekingzcc  
       2017-08-14 14:52:54 +08:00
    lz 可以去搜一下 python 包管理 相对路径 绝对路径 ,搜完之后看看能不能解决~
    lukic
        2
    lukic  
       2017-08-14 15:03:07 +08:00
    app 的 父目录里运行 `python -m unittest tests.test_app`
    messense
        3
    messense  
       2017-08-14 15:05:52 +08:00
    PYTHONPATH=. pytest tests/test_app.py
    cloudyplain
        4
    cloudyplain  
       2017-08-14 15:08:11 +08:00
    py.test
    BiggerLonger
        5
    BiggerLonger  
       2017-08-14 16:42:25 +08:00
    直接上 nose
    guyskk
        6
    guyskk  
       2017-08-14 19:06:11 +08:00   ❤️ 1
    yujianwjj
        7
    yujianwjj  
    OP
       2017-08-14 19:25:34 +08:00
    @guyskk 感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1224 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:08 · PVG 02:08 · LAX 11:08 · JFK 14:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.