def test_nanoha_with_nationality(self): # 消费者服务有"国家"字段的情况 expected = { "salary": 80000, "name": "Takamachi Nahoha", "nationality": "Japan", "contact": { "Email": "*****@*****.**", "Phone Number": "18783723445" } } headers = {"Content-Type": "application/json"} # 通过given去切换ProviderState状态的变化,从而控制Provider端运行测试之前修改对应nationality字段的值 (pact.given('') # 表示生产者服务有国家字段,消费者服务也有用到该字段时,就不需要设置given的值 .upon_receiving('a request for Nanoha').with_request( method='GET', path='/information', query={ 'name': 'nanoha' }).will_respond_with(200, headers, expected)) with pact: result = get_cartoon_characters('nanoha') self.assertEqual(result.json(), expected)
def test_miku(self): # 定义响应的期望结果 expected = { "salary": 45000, "name": "Hatsune Miku", "nationality": "Japan", "contact": { "Email": "*****@*****.**", "Phone Number": "13982739999" } } # 定义响应头 headers = {"Content-Type": "application/json"} # 定义模拟生产者接受请求以及响应方式 (pact.upon_receiving('a request for Miku').with_request( method='GET', path='/information', query={'name': 'miku'}) # 如果是post请求,就是body= .will_respond_with(200, headers, expected)) # 定义消费者服务向模拟生产者发出请求并获得响应 with pact: # 让pact去做请求 result = get_cartoon_characters('miku') # 断言 # self.assertEqual(result.json(), expected) assert result.json(), expected