简体   繁体   English

使用pytest导入单元测试资源:模块级别还是测试级别?

[英]Importing resources for unit testing using pytest: module level or test level?

I have been learning about the advantages of test-driven development, and am attempting to develop my first TDD app using pytest and the setuptools develop option. 我一直在学习测试驱动开发的优势,并尝试使用pytestsetuptools develop选项开发我的第一个TDD应用程序。 Going well so far. 到目前为止一切顺利。 One question I have: where should resources that are to be tested be imported in my test_* modules? 我有一个问题:我的test_*模块中应该将要测试的资源导入到test_*

For example, I could import thusly, at the module level: 例如,我可以在模块级别导入:

from app.module1 import resource1, resource2

def test_resource1():
    assert test_resource1 == "expected value 1"

def test_resource2():
    assert test_resource2 == "expected value 2"

On the other hand, it seems like it makes more sense to do the imports in each test function: 另一方面,在每个测试函数中进行导入似乎更有意义:

def test_resource1():
    from app.module1 import resource1
    assert test_resource1 == "expected value 1"

def test_resource2():
    from app.module1 import resource2
    assert test_resource2 == "expected value 2"

This of course assumes the resources to be tested aren't needed anywhere else. 这当然假定在其他地方不需要要测试的资源。

Aside from the difference in required characters to type, is there an advantage to doing one or the other? 除了所需字符与输入字符的不同之外,做一个或另一个是否有优势?

The standard style guide tells "Imports are always put at the top of the file". 标准样式指南告诉“导入总是放在文件的顶部”。

Function-level imports give no significant architectural or technical benefits. 功能级别的导入不会带来重大的架构或技术优势。

The only valuable exceptions are temporarily inserted breakpoints, which are removed after debugging: import pdb; pdb.set_trace() 唯一有价值的例外是临时插入断点,在调试后删除它们: import pdb; pdb.set_trace() import pdb; pdb.set_trace() or __import__('pdb').set_trace() . import pdb; pdb.set_trace()__import__('pdb').set_trace()

If you import at the function/method level the objects will be in scope for a much shorter time, thus allowing them to be garbage collected much sooner than they otherwise would be. 如果在函数/方法级别导入,则对象将在更短的时间内进入范围,从而允许它们比其他情况更快地进行垃圾收集。 So it's mostly a memory consumption benefit. 所以它主要是内存消耗的好处。

question here is about scope of the test, if test A is intended to cover import of the module, then option B is good. 这里的问题是关于测试的范围,如果测试A旨在涵盖模块的导入,那么选项B是好的。 keep in mind they all gonna fail on importing the same module (if there an issue with that module). 请记住,他们在导入相同模块时都会失败(如果该模块存在问题)。

option is easier most of the time (less typing), cause if you have lots of tests, they won't actully run (you will fail on collect of the test) 选项在大多数情况下更容易(更少打字),因为如果你有很多测试,他们将无法正常运行(你将无法收集测试)

one more thing to fear in this case is side-effects, and both options won't help in that regard, py.test has an option to clear/reload all loaded modules between test (it's not default) 在这种情况下还要担心的另一件事是副作用,这两个选项在这方面都无济于事,py.test有一个选项可以在测试之间清除/重新加载所有加载的模块(它不是默认的)

to summarize, it's more of a reporting style concern. 总而言之,这更像是一种报道风格问题。

Putting imports at the top is a standard convention and doing otherwise (except as noted in other answers for temporary debugging) will make your code less readable to most python developers. 将导入放在顶部是一个标准约定,否则(除了临时调试的其他答案中所述)将使您的代码对大多数python开发人员的可读性降低。

There is no benefit to importing at the class/function level. 在类/功能级别导入没有任何好处。 If you are worried about namespace clashes between objects you are importing then instead of from app.module1 import resource1 do import app.module1 and call app.module1.resource1 in your test. 如果您担心要导入的对象之间的命名空间冲突,那么而不是from app.module1 import resource1 import app.module1并在测试中调用app.module1.resource1 This pattern is also recommended in the python docs Idioms and Anti-Idioms for reasons explained here https://docs.python.org/2/howto/doanddont.html . 在python docs Idioms和Anti-Idioms中也推荐使用这种模式,原因在于https://docs.python.org/2/howto/doanddont.html

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

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