class MockApiServer: def __init__(self, service, log_dir='contract_logs/consumer'): config = SERVICES[service] self.pact = Consumer(BASE_CONFIG['consumer']).has_pact_with( Provider(service), host_name=BASE_CONFIG['host'], port=config['port'], pact_dir=BASE_CONFIG['path_to_pacts'], log_dir=log_dir) for interaction in config['interactions']: (self.pact.given(interaction['GIVEN']).upon_receiving( interaction['UPON_RECEIVING']).with_request( **interaction['REQUEST']).will_respond_with( **interaction['RESPONSE'])) def stop(self): self.pact.stop_service() return self def start(self): self.pact.start_service() self.pact.setup() return self def safe_start(self): self.start() atexit.register(self.stop) return self.pact
def pact(): pact = Consumer(CONSUMER_NAME, tags=['master', 'consumer-py'], version='1.0.0') \ .has_pact_with(Provider(PROVIDER_NAME), # pact_dir=PACT_DIR, //https://github.com/pact-foundation/pact-python/issues/128 version='2.0.0', publish_to_broker=True, broker_base_url=PACT_BROKER_URL) try: pact.start_service() yield pact finally: pact.stop_service()
def pact(request): pact = Consumer('UserServiceClient').has_pact_with( Provider('UserService'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir=PACT_DIR) pact.start_service() yield pact pact.stop_service() version = request.config.getoption('--publish-pact') if not request.node.testsfailed and version: push_to_broker(version)
def pact(request): pact = Consumer('consumer-in-python').has_pact_with( Provider('provider-in-dotnet'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir="./pacts", log_dir="./logs") try: print('start service') pact.start_service() yield pact finally: print('stop service') pact.stop_service()
def _pact_mock_server(): port = 3141 pact_address = socket.gethostbyname(socket.gethostname()) pact = Consumer('TestConsumer').has_pact_with( Provider('TestProvider'), host_name='0.0.0.0', port=port, log_dir="/app" ) pact.start_service() atexit.register(pact.stop_service) server_address_pair = pact, "http://{}:{}".format(pact_address, port) return server_address_pair
def pact(request): version = request.config.getoption('--publish-pact') publish = True if version else False pact = Consumer('UserServiceClient', version=version).has_pact_with( Provider('UserService'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir=PACT_DIR, publish_to_broker=publish, broker_base_url=PACT_BROKER_URL, broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_PASSWORD) print('start service') pact.start_service() yield pact print('stop service') pact.stop_service()
def pact(request): pact = Consumer('UserServiceClient').has_pact_with( Provider('UserService'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir=PACT_DIR, publish_to_broker=False, broker_base_url=PACT_BROKER_URL, broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_USERNAME) pact.start_service() atexit.register(pact.stop_service) yield pact pact.stop_service() version = request.config.getoption('--publish-pact') if not request.node.testsfailed and version: push_to_broker(version) print('not handcraft')
def pact(request): pact = Consumer('PythonConzoomer').has_pact_with( Provider('AccountJSService'), host_name=PROVIDER_HOST, port=PROVIDER_PORT, pact_dir=PACT_DIR) # V: Provider port must align with inner Consumer call setup! (Might be python thing) # V: Therefore I'm pulling definition for local development from Consumer directly try: print('start service') pact.start_service() yield pact finally: print('stop service') pact.stop_service() # version = request.config.getoption('--publish-pact') version = CONSUMER_VERSION # version = False if not request.node.testsfailed and version: push_to_broker(version)
def pact(request): """Setup a Pact Consumer, which provides the Provider mock service. This will generate and optionally publish Pacts to the Pact Broker""" # When publishing a Pact to the Pact Broker, a version number of the Consumer # is required, to be able to construct the compatability matrix between the # Consumer versions and Provider versions version = request.config.getoption("--publish-pact") publish = True if version else False pact = Consumer("UserServiceClient", version=version).has_pact_with( Provider("UserService"), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT, pact_dir=PACT_DIR, publish_to_broker=publish, broker_base_url=PACT_BROKER_URL, broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_PASSWORD, ) pact.start_service() # Make sure the Pact mocked provider is stopped when we finish, otherwise # port 1234 may become blocked atexit.register(pact.stop_service) yield pact # This will stop the Pact mock server, and if publish is True, submit Pacts # to the Pact Broker pact.stop_service() # Given we have cleanly stopped the service, we do not want to re-submit the # Pacts to the Pact Broker again atexit, since the Broker may no longer be # available if it has been started using the --run-broker option, as it will # have been torn down at that point pact.publish_to_broker = False
import atexit import unittest from pact import Consumer, Provider, Term from .consumer import user pact = Consumer('Consumer').has_pact_with(Provider('Provider')) pact.start_service() atexit.register(pact.stop_service) class GetUserInfoContract(unittest.TestCase): def test_get_user(self): expected = { 'username': '******', 'id': 123, 'groups': ['Editors'], 'last_modified': Term(r'\d+-\d+-\d+T\d+:\d+:\d+', '2016-12-15T20:16:01'), } (pact .given('UserA exists and is not an administrator') .upon_receiving('a request for UserA') .with_request('get', '/users/UserA') .will_respond_with(200, body=expected)) with pact: result = user('UserA') expected_result = expected.copy()
# sys.path.append('/Users/yang/PycharmProjects/Test0402_git') # 必须的操作!否则提示 No module named 'MicroService' ******* sys.path.append(os.getcwd()) # 注意:如果要导入该项目其他模块的包名,应将导入的方法写在上面方法的后面 import atexit # 退出时资源自动释放,一般用来做一些资源清理的操作 from atexit import register # 有时候重启pycharm后会找不到atexit模块,也搜不到,需要重新安装一下register import unittest from pact import Consumer, Provider from MicroService.Contract_test.query import get_cartoon_characters import pytest # 构造pact对象,定义消费者服务的名字并给它绑定一个生产者服务 pact = Consumer('Consumer Miku').has_pact_with( Provider('Provider')) # 为消费者绑定一个生产者,名字可随便取 pact.start_service() # 启动pact服务(Start the external Mock Service.) atexit.register(pact.stop_service) # 注册退出时,关闭pact服务,stop_service不能要括号,固定写法 # pact.stop_service() # 由于atexit经常出错,可以不用 class TestGetMikuInfoContract(): def test_miku(self): # 定义响应的期望结果 expected = { "salary": 45000, "name": "Hatsune Miku", "nationality": "Japan", "contact": { "Email": "*****@*****.**", "Phone Number": "13982739999"