zoukankan      html  css  js  c++  java
  • 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)
    
  • 相关阅读:
    程序员创业必读的几本书
    新手上路—Java的"瑞士军刀"
    小团队互联网创业记
    Coder必须自废的两样神功
    码界新手,如何更高效的解决问题
    【转载】FckEditor 2.6.3 for Java 2.4 配置
    struts2上传多文件(b)
    Java基础-Java中的Calendar和Date类
    JAVA加减日期
    Java程序员应该了解的10个面向对象设计原则
  • 原文地址:https://www.cnblogs.com/superhin/p/12770649.html
Copyright © 2011-2022 走看看