如果您正在编写库或应用程序,单元测试文件在哪里?
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:
我更喜欢 #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 单元测试去哪里了?
发布评论