第十期_数据驱动_20191025

课前准备

  • yaml
  • 参数化

yaml

yaml相关lib

配置驱动

def test_read_yaml_file():
    with open("demo.yaml", "r") as f2:
        print(load(f2))

数据驱动

@pytest.mark.parametrize("num", load(open("demo.yaml", "r"))["array"])
def test_param_from_yaml(num):
    assert num>1

类的序列化与反序列化


class HttpApi(yaml.YAMLObject):
    yaml_tag="!HttpApi"
    def __init__(self, method, url, query):
        self.method=method
        self.url=url
        self.query=query

    def send(self):
        return requests.request(self.method, self.url, params=self.query).json()

    def __repr__(self):
        return "%s(method=%r, url=%r, query=%r)" % \\
               (self.__class__.__name__, self.method, self.url, self.query)


def test_write_httpapi():
    h=HttpApi("get", "https://testerhome.com/api/v3/topics.json", {'limit': '2'})
    print()
    print(dump(h, open("/tmp/1", "w")))


def test_read_httpapi():

   req=load(open("/tmp/1", "r"), Loader=yaml.Loader)
   print(req)