简体   繁体   English

Python中测试文件的最佳命名约定是什么?

[英]What is the optimal naming convention for test files in Python?

I am looking for an optimal naming convention for python test files that ease the usage of different test frameworks (unittest, note, pyunit, ...) and also that is friendly with test auto-discovery for these tools. 我正在寻找python测试文件的最佳命名约定,它可以简化不同测试框架(unittest,note,pyunit,...)的使用,并且对于这些工具的测试自动发现也很友好。

I just want a clear set of recomandation that would require minimal configuration for tools. 我只想要一套清晰的再生,这需要最少的工具配置。

  • Tests directory name? 测试目录名称? test or tests ? test还是tests
  • Where to put the tests? 在哪里进行测试? in module/testdir or module/../testdir 在module / testdir或module /../ testdir中
  • Test filenames using underscore or dash? 使用下划线或短划线测试文件名? test_0.py or test-0.py test_0.py或test-0.py

I know, I have too much time :) 我知道,我有太多时间:)

Don't call the directory test or it will conflict with the built-in test package. 不要调用目录test否则它将与内置test包冲突。

The naming conventions are defined in PEP 8 . 命名约定在PEP 8中定义。 See the 'Naming Conventions' section. 请参阅“命名约定”部分。 Underscores are better than hyphens! 下划线比连字符更好!

The layout of your package is a bit more flexible. 包的布局更灵活一些。 I tend to do the following: 我倾向于做以下事情:

package
|-- package
|  |-- __init__.py
|  `-- <etc>
|-- tests
|  `-- <etc>
|-- setup.py
|-- README
|-- LICENCE
`-- <etc>

This keeps the tests separate from the package itself. 这使测试与包本身分开。 Installing the package using setup.py can install just the source, which keeps people's interpreters tidy. 使用setup.py安装软件包可以只安装源代码,这可以使人们的解释器保持整洁。 The tests are there for developers that need them when they get the package source. 对于那些在获得包源时需要它们的开发人员,可以使用这些测试。

You should look at The Hitch Hiker's Guide to Packaging for more info on Python packages. 您应该查看The Hitch Hiker的包装指南 ,了解有关Python包的更多信息。

It will depends of the tool you're using to run your tests. 它取决于您用于运行测试的工具。

If you're using nosetest , the philosophy used to detect test is pretty simple : 如果您正在使用nosetest ,那么用于检测测试的原理非常简单:

If it looks like a test, it's a test. 如果看起来像测试,那就是测试。

If you're using py.test , the conventions are pretty open too . 如果您正在使用py.test ,那么约定也非常开放

About the "where to put test" question, personnaly, I prefer to store tests in a subdirectory in each package to be sure to not forgot to run/touch the tests when someone edit the code ;) 关于“在哪里放测试”的问题,personnaly,我更喜欢将测试存储在每个包的子目录中,以确保在有人编辑代码时不会忘记运行/触摸测试;)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM