简体   繁体   English

在 python 中使用单元测试的正确方法是什么,测试模块位于主 package 内的目录中?

[英]What is the correct way to use unittest in python with the test modules in a directory inside the main package?

I am currently developing a python package with 3.7.1 and am trying to use unittest with a /test directory containing test modules, which will then contain multiple test cases.我目前正在开发带有 3.7.1 的 python package 并尝试将 unittest 与包含测试模块的 /test 目录一起使用,然后该目录将包含多个测试用例。 I am trying to use the following directory structure:我正在尝试使用以下目录结构:

|_ app/
  |_ main.py
  |_ my_module/
             |_ MyPackage.py
  |_ test/
             |_ test_MyPackage.py

Inside test_MyPackage.py I am trying to import the required class from MyPackage using from my_module.MyPackage import MyClass as if the module was placed inside the app/ directory but this does not work as the import fails.在 test_MyPackage.py 中,我正在尝试使用 from my_module.MyPackage import MyClass 从 MyPackage 导入所需的 class,就好像模块放在 app/ 目录中一样,但这不起作用,因为导入失败。 I know that imports in python don't work like this, and tried it after reading the same question at Running unittest with typical test directory structure which appears to be outdated.我知道 python 中的导入不能像这样工作,并在使用似乎已过时的典型测试目录结构运行 unittest阅读了相同的问题后尝试了它。 I have also read that using python -m unittest should work provided that the test cases are in a /test directory and named test_*.py, but this did not work either, and outputs "Ran 0 tests in 0.000s".我还读到使用 python -m unittest 应该可以工作,前提是测试用例位于 /test 目录并命名为 test_*.py,但这也不起作用,并输出“在 0.000 秒内运行 0 个测试”。

So far the only way I'm able to use my unit tests is to move them to the app/ directory each time I want to run them.到目前为止,我能够使用单元测试的唯一方法是每次我想运行它们时将它们移动到 app/ 目录。 Would anybody be able to advise me on the how to use unittests in a separate directory like this without moving them to the app/ directory or importing them with a separate test module permanently inside the app/ directory?是否有人可以建议我如何在这样的单独目录中使用单元测试而不将它们移动到 app/ 目录或将它们与 app/ 目录中的单独测试模块永久导入?

This is working now, I added an __init__.py file in the test/ directory and the tests all ran successfully when using python -m unittest.这现在可以工作了,我在 test/ 目录中添加了一个__init__.py文件,并且在使用 python -m unittest 时测试全部运行成功。 I thought this file wasn't required after Python 3.3 but it appears that unittest doesn't work correctly without it我认为在 Python 3.3 之后不需要此文件,但似乎没有它 unittest 无法正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 unittest.main()以运行包中的所有测试模块 - unittest.main() to run all test modules in a package 在setUp方法中报告Python unittest中的错误的正确方法是什么? - What is the correct way to report an error in a Python unittest in the setUp method? 在python库中导入模块的正确方法是什么? - What is the correct way to import modules in python library? 在 Python 的父目录中使用其他模块的模块(在子目录中)编写单元测试的正确方法是什么 - What is the proper way to write unit tests on modules (in subdirectories) which use other modules in the parent directory in Python 使用Python unittest测试回调调用的正确方法是什么? - What is the right way to test callback invocation using Python unittest? Python 带有 test/ 和 src/ 的单元测试未找到 src 模块 - Python unittest with test/ and src/ not finding src modules 导入python包的正确方法是什么? - What's the correct way to import a python package? Python:要在Unittest中运行Selenium,我需要在程序目录中包括哪些模块? - Python: What modules do I need to include in the directory of my program to run Selenium with Unittest? 在 Python 中测试类的正确方法是什么? - What is the correct way to test classes in Python? Python:写入包中另一个目录的正确方法 - Python: Correct way to write to another directory within a package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM