Python 单元测试去哪里了?

编程入门 行业动态 更新时间:2024-10-24 01:52:33
本文介绍了Python 单元测试去哪里了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果您正在编写库或应用程序,单元测试文件在哪里?

If you're writing a library, or an app, where do the unit test files go?

将测试文件与主应用程序代码分开很好,但将它们放在应用程序根目录内的tests"子目录中很尴尬,因为这会使导入要测试的模块变得更加困难.

It's nice to separate the test files from the main app code, but it's awkward to put them into a "tests" subdirectory inside of the app root directory, because it makes it harder to import the modules that you'll be testing.

这里有最佳实践吗?

推荐答案

对于文件module.py,单元测试一般应该叫test_module.py,如下Pythonic 命名约定.

For a file module.py, the unit test should normally be called test_module.py, following Pythonic naming conventions.

有几个普遍接受的地方放置test_module.py:

There are several commonly accepted places to put test_module.py:

  • 在与 module.py 相同的目录中.
  • 在../tests/test_module.py(与代码目录同级).
  • 在tests/test_module.py(代码目录下的一级).
  • In the same directory as module.py.
  • In ../tests/test_module.py (at the same level as the code directory).
  • In tests/test_module.py (one level under the code directory).
  • 我更喜欢 #1,因为它很容易找到测试并导入它们.您使用的任何构建系统都可以轻松配置为运行以 test_ 开头的文件.实际上,用于测试发现的 默认 unittest 模式是 test*.py.

    I prefer #1 for its simplicity of finding the tests and importing them. Whatever build system you're using can easily be configured to run files starting with test_. Actually, the default unittest pattern used for test discovery is test*.py.

    更多推荐

    Python 单元测试去哪里了?

    本文发布于:2023-11-14 00:00:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1585656.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:去哪里   单元测试   Python

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!