From 47ed588a52264caadcdf9dd21650342e17f262c4 Mon Sep 17 00:00:00 2001 From: Guido Barbaglia Date: Mon, 24 Apr 2017 09:24:52 +1000 Subject: [PATCH 1/5] Setup for TravisCI and Coveralls. --- .gitignore | 8 ++++++++ .travis.yml | 12 ++++++++++++ README.md | 10 +++++++++- dist/.keep | 0 pypact/__init__.py | 5 +++++ pypact/hallo_world.py | 2 ++ requirements.txt | 1 + setup.cfg | 2 ++ setup.py | 17 +++++++++++++++++ tests/__init__.py | 0 tests/test_hallo_world.py | 5 +++++ 11 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 dist/.keep create mode 100644 pypact/__init__.py create mode 100644 pypact/hallo_world.py create mode 100644 requirements.txt create mode 100644 setup.cfg create mode 100644 setup.py create mode 100644 tests/__init__.py create mode 100644 tests/test_hallo_world.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d63ada2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.pyc +.cache/ +.idea/ +build/ +pypact.egg-info/ +tests/__pycache__/ +.eggs/ +.coverage diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ad20551 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: python +python: + - 2.7 +install: + - "pip install -r requirements.txt" + - "pip install pytest pytest-cov" + - "pip install coveralls" + - "pip install -e ." +script: + - py.test --cov pypact --cov-report term-missing +after_success: + - coveralls diff --git a/README.md b/README.md index a678dfe..c0344d2 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ -# pypact +[![Build Status](https://travis-ci.org/Kalimaha/pypact.svg?branch=master)](https://travis-ci.org/Kalimaha/pypact) +[![Coverage Status](https://coveralls.io/repos/github/Kalimaha/pypact/badge.svg?branch=master)](https://coveralls.io/github/Kalimaha/pypact?branch=master) + +# PyPact Python implementation for Pact (http://pact.io/) + +## Setup +``` +python setup.py install +``` diff --git a/dist/.keep b/dist/.keep new file mode 100644 index 0000000..e69de29 diff --git a/pypact/__init__.py b/pypact/__init__.py new file mode 100644 index 0000000..5508e43 --- /dev/null +++ b/pypact/__init__.py @@ -0,0 +1,5 @@ +import os +import sys + +source_path = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, source_path + '/../') diff --git a/pypact/hallo_world.py b/pypact/hallo_world.py new file mode 100644 index 0000000..e223df3 --- /dev/null +++ b/pypact/hallo_world.py @@ -0,0 +1,2 @@ +def func(n): + return n + 2 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e079f8a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..9af7e6f --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[aliases] +test=pytest \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..4833195 --- /dev/null +++ b/setup.py @@ -0,0 +1,17 @@ +from setuptools import setup +from setuptools import find_packages + +setup( + name='pypact', + version='0.1.0', + author='Guido Barbaglia', + author_email='guido.barbaglia@gmail.com', + packages=find_packages(), + license='LICENSE.txt', + long_description=open('README.md').read(), + description='Python implementation for Pact (http://pact.io/)', + install_requires=[], + setup_requires=['pytest-runner'], + tests_require=['pytest'], + url='https://github.com/Kalimaha/pypact/' +) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_hallo_world.py b/tests/test_hallo_world.py new file mode 100644 index 0000000..4dd2a8c --- /dev/null +++ b/tests/test_hallo_world.py @@ -0,0 +1,5 @@ +from pypact.hallo_world import func + + +def test_answer(): + assert func(3) == 5 From bac33bbf62dd4079d0d8625b45245358981e9476 Mon Sep 17 00:00:00 2001 From: Guido Barbaglia Date: Thu, 27 Apr 2017 12:51:28 +1000 Subject: [PATCH 2/5] Travis --- .gitignore | 1 + requirements.txt | 2 +- setup.cfg | 2 -- setup.py | 5 +++-- tests/test_hallo_world.py | 7 +++++-- 5 files changed, 10 insertions(+), 7 deletions(-) delete mode 100644 setup.cfg diff --git a/.gitignore b/.gitignore index d63ada2..5c02868 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ pypact.egg-info/ tests/__pycache__/ .eggs/ .coverage +*.egg diff --git a/requirements.txt b/requirements.txt index e079f8a..cafd3e0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -pytest +nose2 diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 9af7e6f..0000000 --- a/setup.cfg +++ /dev/null @@ -1,2 +0,0 @@ -[aliases] -test=pytest \ No newline at end of file diff --git a/setup.py b/setup.py index 4833195..e6a1def 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,8 @@ long_description=open('README.md').read(), description='Python implementation for Pact (http://pact.io/)', install_requires=[], - setup_requires=['pytest-runner'], - tests_require=['pytest'], + # setup_requires=['pytest-runner'], + tests_require=['nose2'], + test_suite='nose2.collector.collector', url='https://github.com/Kalimaha/pypact/' ) diff --git a/tests/test_hallo_world.py b/tests/test_hallo_world.py index 4dd2a8c..efcbacf 100644 --- a/tests/test_hallo_world.py +++ b/tests/test_hallo_world.py @@ -1,5 +1,8 @@ +import unittest from pypact.hallo_world import func -def test_answer(): - assert func(3) == 5 +class TestHalloWorld(unittest.TestCase): + + def test_answer(self): + assert func(3) == 5 \ No newline at end of file From db51cb326cc69688ccd8034dcc52f6aec5a9bceb Mon Sep 17 00:00:00 2001 From: Guido Barbaglia Date: Thu, 27 Apr 2017 13:42:39 +1000 Subject: [PATCH 3/5] Revert "Travis" --- .gitignore | 1 - requirements.txt | 2 +- setup.cfg | 2 ++ setup.py | 5 ++--- tests/test_hallo_world.py | 7 ++----- 5 files changed, 7 insertions(+), 10 deletions(-) create mode 100644 setup.cfg diff --git a/.gitignore b/.gitignore index 5c02868..d63ada2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,3 @@ pypact.egg-info/ tests/__pycache__/ .eggs/ .coverage -*.egg diff --git a/requirements.txt b/requirements.txt index cafd3e0..e079f8a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -nose2 +pytest diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..9af7e6f --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[aliases] +test=pytest \ No newline at end of file diff --git a/setup.py b/setup.py index e6a1def..4833195 100644 --- a/setup.py +++ b/setup.py @@ -11,8 +11,7 @@ long_description=open('README.md').read(), description='Python implementation for Pact (http://pact.io/)', install_requires=[], - # setup_requires=['pytest-runner'], - tests_require=['nose2'], - test_suite='nose2.collector.collector', + setup_requires=['pytest-runner'], + tests_require=['pytest'], url='https://github.com/Kalimaha/pypact/' ) diff --git a/tests/test_hallo_world.py b/tests/test_hallo_world.py index efcbacf..4dd2a8c 100644 --- a/tests/test_hallo_world.py +++ b/tests/test_hallo_world.py @@ -1,8 +1,5 @@ -import unittest from pypact.hallo_world import func -class TestHalloWorld(unittest.TestCase): - - def test_answer(self): - assert func(3) == 5 \ No newline at end of file +def test_answer(): + assert func(3) == 5 From 3d99bba30767cbe7f4b3f8b51ae7a8facbb5007a Mon Sep 17 00:00:00 2001 From: Guido Barbaglia Date: Thu, 27 Apr 2017 13:49:59 +1000 Subject: [PATCH 4/5] Configure pytest-sugar. --- .travis.yml | 2 +- requirements.txt | 1 + setup.py | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index ad20551..0dd7d47 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ python: - 2.7 install: - "pip install -r requirements.txt" - - "pip install pytest pytest-cov" + - "pip install pytest pytest-cov pytest-sugar" - "pip install coveralls" - "pip install -e ." script: diff --git a/requirements.txt b/requirements.txt index e079f8a..1bf5204 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ pytest +pytest-sugar diff --git a/setup.py b/setup.py index 4833195..15d1177 100644 --- a/setup.py +++ b/setup.py @@ -12,6 +12,6 @@ description='Python implementation for Pact (http://pact.io/)', install_requires=[], setup_requires=['pytest-runner'], - tests_require=['pytest'], + tests_require=['pytest', 'pytest-sugar'], url='https://github.com/Kalimaha/pypact/' ) From e0e1bd87509c72a504d345f1ec9897acd1166766 Mon Sep 17 00:00:00 2001 From: Kalimaha Date: Thu, 27 Apr 2017 19:21:12 +1000 Subject: [PATCH 5/5] Investigation and experiments with PyTest. --- .gitignore | 5 ++++ .travis.yml | 6 +++- README.md | 4 +-- pypact/__init__.py | 5 ---- pypact/hallo_world.py | 2 -- pytest_pact/PyPact.py | 56 +++++++++++++++++++++++++++++++++++++ pytest_pact/__init__.py | 0 requirements.txt | 3 +- setup.cfg | 2 +- setup.py | 9 ++++-- tests/test_books_service.py | 23 +++++++++++++++ tests/test_hallo_world.py | 5 ---- tests/test_pypact.py | 22 +++++++++++++++ 13 files changed, 122 insertions(+), 20 deletions(-) delete mode 100644 pypact/__init__.py delete mode 100644 pypact/hallo_world.py create mode 100644 pytest_pact/PyPact.py create mode 100644 pytest_pact/__init__.py create mode 100644 tests/test_books_service.py delete mode 100644 tests/test_hallo_world.py create mode 100644 tests/test_pypact.py diff --git a/.gitignore b/.gitignore index d63ada2..d254fc4 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,8 @@ pypact.egg-info/ tests/__pycache__/ .eggs/ .coverage +*.log +*.egg +*.iml +pytest_pact.egg-info/ +pytest_pact/__pycache__ diff --git a/.travis.yml b/.travis.yml index 0dd7d47..5bfd00c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,16 @@ language: python python: - 2.7 + - 3.3 + - 3.4 + - 3.5 + - 3.6 install: - "pip install -r requirements.txt" - "pip install pytest pytest-cov pytest-sugar" - "pip install coveralls" - "pip install -e ." script: - - py.test --cov pypact --cov-report term-missing + - py.test --cov pytest_pact --cov-report term-missing after_success: - coveralls diff --git a/README.md b/README.md index c0344d2..d755fb3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -[![Build Status](https://travis-ci.org/Kalimaha/pypact.svg?branch=master)](https://travis-ci.org/Kalimaha/pypact) -[![Coverage Status](https://coveralls.io/repos/github/Kalimaha/pypact/badge.svg?branch=master)](https://coveralls.io/github/Kalimaha/pypact?branch=master) +[![Build Status](https://travis-ci.org/Kalimaha/pytest-pact.svg?branch=master)](https://travis-ci.org/Kalimaha/pytest-pact) +[![Coverage Status](https://coveralls.io/repos/github/Kalimaha/pytest-pact/badge.svg?branch=master)](https://coveralls.io/github/Kalimaha/pytest-pact?branch=master) # PyPact Python implementation for Pact (http://pact.io/) diff --git a/pypact/__init__.py b/pypact/__init__.py deleted file mode 100644 index 5508e43..0000000 --- a/pypact/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -import os -import sys - -source_path = os.path.dirname(os.path.abspath(__file__)) -sys.path.insert(0, source_path + '/../') diff --git a/pypact/hallo_world.py b/pypact/hallo_world.py deleted file mode 100644 index e223df3..0000000 --- a/pypact/hallo_world.py +++ /dev/null @@ -1,2 +0,0 @@ -def func(n): - return n + 2 diff --git a/pytest_pact/PyPact.py b/pytest_pact/PyPact.py new file mode 100644 index 0000000..f23d6da --- /dev/null +++ b/pytest_pact/PyPact.py @@ -0,0 +1,56 @@ +import pytest + + +state = pytest.mark.state +given = pytest.mark.given +base_uri = pytest.mark.base_uri +pact_uri = pytest.mark.pact_uri +with_request = pytest.mark.with_request +has_pact_with = pytest.mark.has_pact_with +upon_receiving = pytest.mark.upon_receiving +will_respond_with = pytest.mark.will_respond_with +service_consumer = pytest.mark.service_consumer +honours_pact_with = pytest.mark.honours_pact_with + + +@pytest.hookimpl(hookwrapper=True) +def pytest_pyfunc_call(pyfuncitem): + # print(describe_consumer_pact(pyfuncitem)) + + # pypact = PyPact() + # pypact.executor(pyfuncitem) + + + + outcome = yield + # outcome.excinfo may be None or a (cls, val, tb) tuple + print(outcome) + + res = outcome.get_result() # will raise if outcome was exception + # postprocess result + print(res) + + +def consumer_or_provider(pyfuncitem): + pass + +class PyPactConsumer(object): + pass + +class PyPactProvider(object): + pass + + +def describe_consumer_pact(pyfuncitem): + s = '' + s += 'Given ' + read_marker(pyfuncitem, 'given') + ', ' + s += 'upon receiving ' + read_marker(pyfuncitem, 'upon_receiving') + ' ' + s += 'from ' + read_marker(pyfuncitem, 'service_consumer') + ' ' + s += 'with:\n\n' + read_marker(pyfuncitem, 'with_request') + '\n\n' + s += read_marker(pyfuncitem, 'has_pact_with') + ' will respond with:\n\n' + s += read_marker(pyfuncitem, 'will_respond_with') + return s + +def read_marker(pyfuncitem, marker_name): + marker = pyfuncitem.get_marker(marker_name) + return str(marker.args[0]) if marker else '' diff --git a/pytest_pact/__init__.py b/pytest_pact/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt index 1bf5204..aa815b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ -pytest -pytest-sugar +pytest>=3.0 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 9af7e6f..b7e4789 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,2 @@ [aliases] -test=pytest \ No newline at end of file +test=pytest diff --git a/setup.py b/setup.py index 15d1177..7bda334 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import find_packages setup( - name='pypact', + name='pytest-pact', version='0.1.0', author='Guido Barbaglia', author_email='guido.barbaglia@gmail.com', @@ -13,5 +13,10 @@ install_requires=[], setup_requires=['pytest-runner'], tests_require=['pytest', 'pytest-sugar'], - url='https://github.com/Kalimaha/pypact/' + url='https://github.com/Kalimaha/pytest-pact/', + entry_points = { + 'pytest11': [ + 'pytest-pact = pytest_pact.PyPact', + ] + } ) diff --git a/tests/test_books_service.py b/tests/test_books_service.py new file mode 100644 index 0000000..fc4b6ee --- /dev/null +++ b/tests/test_books_service.py @@ -0,0 +1,23 @@ +from pytest_pact.PyPact import * + + +@base_uri('localhost:1234') +@service_consumer('Library App') +@has_pact_with('Books Service') +class TestBooksService(): + + expected_response = { + 'status': 200, + 'headers': {'Content-Type': 'application/json'}, + 'body': { + 'id': '123', + 'title': 'A Fortune-Teller Told Me' + } + } + + @given('some books exist') + @upon_receiving('a request for a book') + @with_request({'method': 'get', 'path': '/books/123'}) + @will_respond_with(expected_response) + def test_get_book(self): + pass diff --git a/tests/test_hallo_world.py b/tests/test_hallo_world.py deleted file mode 100644 index 4dd2a8c..0000000 --- a/tests/test_hallo_world.py +++ /dev/null @@ -1,5 +0,0 @@ -from pypact.hallo_world import func - - -def test_answer(): - assert func(3) == 5 diff --git a/tests/test_pypact.py b/tests/test_pypact.py new file mode 100644 index 0000000..5046f60 --- /dev/null +++ b/tests/test_pypact.py @@ -0,0 +1,22 @@ +from pytest_pact.PyPact import * + + +def test_read_existing_marker(): + expected_marker = 'some books exist' + class FakeMarker(object): + args = [expected_marker] + class FakePyFuncItem(object): + def get_marker(self, marker_name): + return FakeMarker() + pyfuncitem = FakePyFuncItem() + marker_name = 'given' + assert read_marker(pyfuncitem, marker_name) == expected_marker + + +def test_read_non_existing_marker(): + class FakePyFuncItem(object): + def get_marker(self, marker_name): + return None + pyfuncitem = FakePyFuncItem() + marker_name = 'given' + assert read_marker(pyfuncitem, marker_name) == ''