【问题标题】:Module Import Error running py.test with modules on Path使用路径上的模块运行 py.test 的模块导入错误
【发布时间】:2013-06-05 16:24:52
【问题描述】:

我无法以我想要的方式导入我的模块进行测试。我在 2.7.2 的 virtualenv 中运行所有这些

我有一个类似的目录结构

/api
    /api
        __init__.py
        my_module.py
    /tests
        my_module_test.py

我将 PYTHONPATH 设置为 /Path/api/。我将 CD 放入 /Path/api 并运行以下命令

 py.test tests/my_module_test.py   

在以下情况下不起作用:

  1. 当我在 my_module_test.py 的顶部有以下内容时 from api.my_module import my_function

它确实适用于以下情况:

  1. 当我在 my_module_test.py 的顶部有以下内容时 from my_module import my_function

为什么我不能像案例 1 那样导入我的模块?

【问题讨论】:

    标签: python python-2.7 pythonpath pytest


    【解决方案1】:

    我使用 PYTHONPATH 作为

    PYTHONPATH=`pwd` py.test tests/my_module_test.py
    

    【讨论】:

      【解决方案2】:

      从py.text文件,你应该先安装。:

      pip install -e .
      

      【讨论】:

        【解决方案3】:

        我创建这个是为了回答您的问题和我自己的困惑。我希望它有所帮助。注意PYTHONPATHpy.test 命令行和tox.ini 中。

        示例项目是here,下面还有:

        mymodule.py:

        import boto3
        
        def stuff():
            print "Yep!"
        

        tests/text_syntax_errors.py:

        import boto3
        import mymodule
        
        
        # Define a basic test that actually doesn't do much. 
        # I just wanted more than zero tests
        def test_one_equals_one():
            assert 1 == 1
        

        tox.ini:

        [tox]
        skipsdist = True
        envlist = py27
        
        [flake8]
        max-line-length = 119
        
        [testenv]
        deps= -r{toxinidir}/requirements.txt
        commands=py.test
        setenv =
            PYTHONPATH = {toxinidir}
        

        requirements.txt:

        boto3
        pytest
        

        来自我的README.md

        如何运行这些示例

        我测试代码的最初动机是我在为工作编写的脚本中拼错了一个导入的模块。

        如果您编辑mymodule.py 并从“boto3”中删除b,您将看到以下命令失败。这是一件好事。同样,如果您希望看到实际测试失败,只需编辑 tests/test_syntax_errors.py 并将 1 == 1 更改为 1 == 0

        py.test

        mbp0 pytest_test[master+*] $ PYTHONPATH=. py.test
        ========================== test session starts ==========================
        platform darwin -- Python 2.7.11, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
        rootdir: /Users/jmacdonald/w/pytest_test, inifile:
        collected 1 items
        
        tests/test_syntax_errors.py .
        
        ======================= 1 passed in 0.11 seconds ========================
        mbp0 pytest_test[master+*] $
        

        mbp0 pytest_test[master+*] $ tox
        py27 installed: boto3==1.3.1,botocore==1.4.37,docutils==0.12,futures==3.0.5,jmespath==0.9.0,py==1.4.31,pytest==2.9.2,python-dateutil==2.5.3,six==1.10.0
        py27 runtests: PYTHONHASHSEED='713732044'
        py27 runtests: commands[0] | py.test
        ========================== test session starts ==========================
        platform darwin -- Python 2.7.11, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
        rootdir: /Users/jmacdonald/w/pytest_test, inifile:
        collected 1 items
        
        tests/test_syntax_errors.py .
        
        ======================= 1 passed in 0.11 seconds ========================
        ________________________________ summary ________________________________
          py27: commands succeeded
          congratulations :)
        mbp0 pytest_test[master+*] $
        

        【讨论】:

        • 这是我在 StackOverflow 上的第一个答案。你能详细说明你的意思吗?我的代码是这个问题的具体答案。在文本区域中发布有点过于复杂。
        • @JeffMacDonald 有一个特殊的代码格式,你可以使用它! :)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-18
        • 2012-04-27
        • 2017-01-15
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        相关资源
        最近更新 更多