python – 使用pytest导入单元测试的资源:模块级别或测试级别?

我一直在学习测试驱动开发的优势,并尝试使用pytestsetuptools开发选项开发我的第一个TDD应用程序.到目前为止一切顺利.我有一个问题:我的test_ *模块中应该将要测试的资源导入到哪里?

例如,我可以在模块级别导入:

from app.module1 import resource1, resource2

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

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

另一方面,在每个测试函数中进行导入似乎更有意义:

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"

这当然假定在其他地方不需要要测试的资源.

除了所需字符与输入字符的不同之外,做一个或另一个是否有优势?

解决方法:

standard style guide告诉“导入总是放在文件的顶部”.

功能级别的导入不会带来重大的架构或技术优势.

唯一有价值的例外是临时插入断点,在调试后删除它们:import pdb; pdb.set_trace()或__import __(‘pdb’).set_trace().

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐