lundi 30 mai 2016

which is the best python framework concept? for unitesting - change only config

i'm a tester; not a programmer :) i'm trying to make a python framework with which you can run any created test according to it's config.

the dir structure:

project
\runtest.py
\lib\__init__.py
\lib\cfg_parser.py
\lib\cli.py
\lib\main.py
\testscripts\__init__.py
\testscripts\test1.py
\testscripts\default.cfg

runtest.py > is ment to be a option parser:

python runtest.py --path <test path> --test <test name without .py>

(cfg name should be always default.cfg)

from unittest import TestCase,TestLoader,TextTestRunner
path_files=options.path_files
file_test=options.file_test

exec("suite = TestLoader().loadTestsFromTestCase(%s.%s)" %(file_test,file_test))
TextTestRunner(verbosity=2).run(suite)

test1.py > use to run steps defined using values from dictionaries created from config using config_parser

from unittest import TestCase,TestLoader,TextTestRunner
from lib.cfg_parser import *

class TestScript(TestCase):
    def step1...

default.cfg >

[DEVICE1]
ip_ts = 192.168.0.2
port_ts = 6046
user=admin
pass=admin

cfg_parser.py > used to create global dictionaries >>> just call DEVICE1['ip_ts'] for ip for example

from ConfigParser import SafeConfigParser
from main import file_cfg >>> file_cfg should have path+default.cfg

def cfg_parser(file):
    parser = SafeConfigParser()
    parser.read(file)

    list_dict=[]
    list_sut=[]

    for sut_name in parser.sections():
        list_sut.append(sut_name)
        dictt={}
        for name, value in parser.items(sut_name):
            dictt[name] = value 
        globals()[sut_name]=dictt

        list_dict.append(globals()[sut_name])
    return (list_sut, list_dict)

list_sut, list_dict = cfg_parser(file_cfg)
for element in list_sut:
    j=list_sut.index(element)
    exec("global %s" %element)
    exec("%s = %s" %(element,list_dict[j]))

cli.py >>> class with methods used to reach de device via serial/telnet etc.

main.py >>> should be used to store path, test, config name etc. so can be runned by test1.py,cfg_parser.py and all scripts with init.py created in its dir

i'm stumped at>

  1. how to pass from runtest.py the variables introduced by the user to main.py so they can reach cfg_parser and test1? (test1 needs cfg data to be ran)
  2. i observed that if i change the python path... the created modules/methods are no longer recognized.... how could I fix this?

hope i was clear enough :d

my logic is:

  1. i ran runtest.py and give options: --path /project/testscripts/ --test test1 later i wanna make options for os (linux/windows)
  2. test1.py should be ran with the values from default.cfg
  3. values should be obtained with cfg_parser help

any help about the flaws/mistakes of my concept is greatly appreciated

thank you :)

Aucun commentaire:

Enregistrer un commentaire