Robot Framework简易复刻版-未完成

data.yaml

Settings:
  Documention: 测试套件
  Library:
    - Selenium2Library
    - requestLibrary
  Resouce:
    - abc.txt
  ForceTags: [abc]
  DefaultTags: [p2]
  TestTimeout: 60

Variables:
  a: 3
  b: 5

Keywords:
  open:
    Arguments: [a,b]
    Steps:
      - log a
      - log b
      - set c=a+b
    Return: c

TestCases:
  - 测试用例1:
      Documention: 测试用例1描述
      Tags: p3
      Timeout: 30
      Steps:
        - log hello,$a
  - 测试用例2:
      Documention: 测试用例2描述
      Tags: p3
      Timeout: 30
      Steps:
        - log hello,$b

robotz.py

import unittest
import types
from string import Template


from logz import log
from filez import file

print = log.info

functions = dict(
    log=log.info
)

def parse_dollar(context: dict, expr: str) -> str:
    """解析$变量"""
    return Template(expr).safe_substitute(context) if '$' in expr else expr


def do_step(context, expr: str):
    expr = parse_dollar(context, expr)
    func_name, *args = expr.split()
    func = functions.get(func_name)
    func(*args)


def build_keyword(keyword: dict)->types.FunctionType:
    name, attrs = tuple(keyword.items())[0]
    pass



def build_case(index:int, test: dict)->types.FunctionType:
    name, attrs = tuple(test.items())[0]

    def test_method(self):
        steps = attrs.get('Steps')
        for step in steps:
            do_step(self.context, step)

    test_method.__name__ = f'test_{index+1}'
    test_method.__doc__ = attrs.get('Documention')
    test_method.name = name   # 使用自定义runner支持
    test_method.tags = attrs.get('Tags')  # 使用自定义runner支持
    test_method.timeout = attrs.get('Timeout')  # 使用自定义runner支持
    return test_method


def build(data: dict):
    context = variables = data.get('Variables')

    class TestRobot(unittest.TestCase):
        @classmethod
        def setUpClass(cls) -> None:
            cls.context = context

    tests = data.get('TestCases')
    [setattr(TestRobot, f'test_{index+1}', build_case(index, test)) for index, test in enumerate(tests)]

    suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestRobot)
    return suite


def run(suite):
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suite)


if __name__ == '__main__':
    # text = 'log hello,world'
    # do_step(text)
    data = file.load('/Users/apple/Documents/Projects/Self/PyPi/runnerz/robots/data.yaml')
    suite = build(data)
    run(suite)

猜你喜欢

转载自www.cnblogs.com/superhin/p/12770649.html