【问题标题】:pytest: how to make dedicated test directorypytest:如何制作专用的测试目录
【发布时间】:2023-03-24 21:28:01
【问题描述】:

我想要以下项目结构:

|--folder/
|  |--tests/
|  |--project/

让我们写一个简单的例子:

|--test_pytest/
|  |--tests/
|  |  |--test_sum.py
|  |--t_pytest/
|  |  |--sum.py
|  |  |--__init__.py

sum.py:

def my_sum(a, b):
    return a + b

test_sum.py:

from t_pytest.sum import my_sum
def test_my_sum():
    assert my_sum(2, 2) == 5, "math still works"

让我们运行它:

test_pytest$ py.test ./
========== test session starts ===========
platform linux -- Python 3.4.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/step/test_pytest, inifile: 
collected 0 items / 1 errors 

================= ERRORS =================
___ ERROR collecting tests/test_sum.py ___
tests/test_sum.py:1: in <module>
    from t_pytest import my_sum
E   ImportError: No module named 't_pytest'
======== 1 error in 0.01 seconds =========

它看不到 t_pytest 模块。它是像 httpie 一样制作的:

https://github.com/jkbrzt/httpie/

https://github.com/jkbrzt/httpie/blob/master/tests/test_errors.py

为什么?我该如何纠正?

【问题讨论】:

  • t_pytest 是否已安装或在导入路径上?
  • import t_pytest一般工作得好吗?如果不是,这就是它对测试不起作用的原因。
  • 我想,不。导入路径上的含义是什么? sys.patch 中的 t_pytest?
  • 谢谢,我想我明白了。

标签: python testing pytest project-structure


【解决方案1】:

另一种方法是将测试也变成一个模块,方法是在文件夹测试中添加 __init__.py 文件。最流行的 python 库请求之一https://github.com/kennethreitz/requests 在他们的单元测试文件夹测试中以这种方式进行。您不必将 PYTHONPATH 导出到您的项目来执行测试。

限制是您必须在示例项目的目录“test_pytest”中运行 py.test 命令。

还有一件事,您的示例代码中的导入行是错误的。应该是

from t_pytest.sum import my_sum

【讨论】:

    【解决方案2】:

    谢谢你,jonrsharpe。 Py.test 没有魔法,我需要自己让我的包可以导入。有一种可能的解决方案:

       $ export PYTHONPATH="${PYTHONPATH}: [Path to folder with my module]"   
    

    (PYTHONPATH 是 sys.path 的路径来源之一)
    如果我需要永久进行此更改,我需要将此字符串添加到~/.bashrc

    【讨论】:

    • 更简洁的方法是设置一个virtualenv 并在其中安装 pytest 和您的包 (pip install -e .)。
    【解决方案3】:

    使用 pytest:

    if __name__ == '__main__':
         import pytest
         pytest.main(['-x', 'tests', '--capture', 'sys'])
    
    # Note: tests is the directory name
    

    这可能有助于解决您的问题使用单元测试:

    假设我们有一个名为run_tests.py的启动脚本

    run_tests.py中的代码:

    import unittest
    
    if __name__ == '__main__':
        # use the default shared TestLoader instance
        test_loader = unittest.defaultTestLoader
    
        # use the basic test runner that outputs to sys.stderr
        test_runner = unittest.TextTestRunner()
    
        # automatically discover all tests in the current dir of the form test*.py
        # NOTE: only works for python 2.7 and later
        test_suite = test_loader.discover('./tests')
    
        # run the test suite
        test_runner.run(test_suite)
    

    您只需将./tests 替换为您的顶级目标目录(绝对路径)包含所有测试脚本

    你只需像这样运行这个文件

    python run_tests.py

    您将能够看到正在运行的所有测试脚本。

    【讨论】:

    • 问题是关于 py.test,这是 unittest.py ;)
    【解决方案4】:

    试试这个:

    # in bash/zsh shell
    test_pytest$ PYTHONPATH=. pytest
    
    # in fish shell
    test_pytest$ env PYTHONPATH=. pytest
    

    它与@stepuncius points atthis answera gist 中所指的相同。我只是想把它放在一个干净的答案中。是小项目的简单解决方案,无需干预包模块结构,或setup.py等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-23
      • 2022-12-09
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      相关资源
      最近更新 更多