简体   繁体   English

如何在鼻子测试中将变量从设置传递到测试?

[英]How to pass variables from setup to tests in nosetest?

With python and nosetests I have the following setup: 使用python和nosetest,我有以下设置:

- package
    - __init__.py
    - test1.py
    - test2.py

The __init__.py module contains a set up function __init__.py模块包含一个设置功能

def setup():
    print("Setup called")
    var = 42

which will be used later to create a unique identified (different between running the tests, but the same for all the tests inside the package). 稍后将使用它来创建唯一的标识(运行测试之间有所不同,但包内的所有测试都相同)。

How can the tests itself access this variable (in this example case var )? 测试本身如何访问此变量(在本例中为var )? The test scripts are just some stubs: 测试脚本只是一些存根:

from nose.tools import assert_true

class TestSuite(object):
    def test1(self):
        # How to get content of 'var' here?
        assert_true(True)

Is there some pythonic way to do this, or just use an environment variable to do this? 有一些pythonic的方法可以做到这一点,还是只使用环境变量来做到这一点?

nose calls .setup() methods inside classes: 在类内部调用.setup()方法:

class Test:
    def setup(self):
        self.var = 1

    def test_print_var(self):
        print(self.var)

This also applies to methods inherited from elsewhere: 这也适用于从其他地方继承的方法:

class TestBase:
    def setup(self):
        self.var = 1

class Test(TestBase):
    def test_print_var(self):
        print(self.var)

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

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