【问题标题】:Python keeping common code inside filePython将通用代码保存在文件中
【发布时间】:2016-02-03 11:52:39
【问题描述】:

我有一些通用代码用于我的所有 PyTest。我想将完整的代码保存在任何文件中并在运行时加载它。可以用 Python 吗?

例如:

def teardown_method(self, method):
 print "This is tear down method"
def setup_method(self, method):
 print "This is setup method"

【问题讨论】:

    标签: python pytest pytest-django


    【解决方案1】:

    除了简单地使用import,对于您展示的示例(设置/拆卸),您通常会改用pytest fixture,并将它们放在conftest.py 中以在测试之间共享它们。

    【讨论】:

    • 是的,我正在将竞赛.py 用于一些常用功能。在我的情况下,我需要使用函数更新我的 TestClass 的 bTestStatus 变量。我如何从 conftest.py 访问那些 TestClass 变量 例如: class TestClass1: bTestStatus = False def teardown_method(self, method): print "This is tear down method" TestClass1.bTestStatus = True def setup_method(self, method): print "这是设置方法"
    • 还有我如何在比赛中移动 setup 和 Teardown 方法
    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多