V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Aug 14, 2017 · 2958 views
    This topic created in 3184 days ago, the information mentioned may be changed or developed.

    项目结构如下:

    ├── 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 replies    2017-08-14 19:25:34 +08:00
    pekingzcc
        1
    pekingzcc  
       Aug 14, 2017
    lz 可以去搜一下 python 包管理 相对路径 绝对路径 ,搜完之后看看能不能解决~
    lukic
        2
    lukic  
       Aug 14, 2017
    app 的 父目录里运行 `python -m unittest tests.test_app`
    messense
        3
    messense  
       Aug 14, 2017
    PYTHONPATH=. pytest tests/test_app.py
    cloudyplain
        4
    cloudyplain  
       Aug 14, 2017
    py.test
    BiggerLonger
        5
    BiggerLonger  
       Aug 14, 2017
    直接上 nose
    guyskk
        6
    guyskk  
       Aug 14, 2017   ❤️ 1
    yujianwjj
        7
    yujianwjj  
    OP
       Aug 14, 2017
    @guyskk 感谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2479 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 08:42 · PVG 16:42 · LAX 01:42 · JFK 04:42
    ♥ Do have faith in what you're doing.