从各种测试文件中访问插件定义的数据

时间:2014-12-01 17:23:21

标签: python unit-testing python-2.7 nose nosetests

我正在尝试在我的python代码中实现一组运行形式的鼻子测试。

我正在调用此文件中的nose run方法,并根据example from the nose docs添加自定义插件:

import os
import sys
import outer_packages
import nose
from nose.plugins import Plugin
import misc_methods
from rednose import RedNose
import termstyle

class CTRexScenario():
    configuration = None
    trex          = None
    platform      = None

class CTRexTestConfiguringPlugin(Plugin):
    def options(self, parser, env={}):
        parser.add_option('--trex-scenario-config', action='store',
                            dest='config_path', default='config/config.yaml',
                            help='Specify path to the T-Rex testing scenario, defining T-Rex, platform and a TFTP server. Default is /config/config.yaml')

    def configure(self, options, conf):
        if options.config_path:
            self.configuration = misc_methods.load_complete_config_file(options.config_path)
            self.enabled = True

    def begin (self):
        CTRexScenario.configuration = self.configuration

def set_report_dir (report_dir):
    if not os.path.exists(report_dir):
        os.mkdir(report_dir)

if __name__ == "__main__":

    # setting defaults. By default we run all the test suite
    specific_tests   = False;
    long_test       = False
    report_dir      = "reports"

    for arg in sys.argv:
        if 'unit_tests/' in arg:
            specific_tests = True;

    nose_argv= sys.argv + ['-s','-v','--exe', '--rednose', '--with-xunit','--xunit-file=' + report_dir + "/unit_test.xml"]

    # Run all of the unit tests or just the selected ones
    if not specific_tests:
        nose_argv += ['unit_tests']

    result = nose.run(argv = nose_argv, addplugins = [RedNose(), CTRexTestConfiguringPlugin()])#, plugins=[CConfiguringPlugin()])

    if result == True:
        print termstyle.green("passed!")
        sys.exit(0)
    else:
        sys.exit(-1);

现在,我想创建一个通用测试用例,从中包含此方案,以便所有以下测试都有相同的共享方案。 ie-继承一般情况的所有测试都将基于相同的setUptearDown方法。

这是一般测试文件:

from nose.plugins import Plugin
import trex
import misc_methods
import sys

class CTRexGeneral_Test():
    """This class defines the general testcase of the T-Rex traffic generator"""

    def setUp(self):
        self.a = CTRexScenario()
        ##### Here I want to config the rest of the scenario, based on the passed configuration file

    def test_can_frobnicate(self):
        assert (self.a.configuration==None)
        # assert 2*5==10

    def tearDown(self):
        pass

不幸的是,我不明白应该如何从常规测试文件中访问已获取的配置(已经由插件解析)

谢谢!

0 个答案:

没有答案