1+ import atexit
2+
13import requests
24import unittest
35
68from pact .provider import Provider
79
810
11+ pact = Consumer ('consumer' ).has_pact_with (Provider ('provider' ))
12+ pact .start_service ()
13+ atexit .register (pact .stop_service )
14+
15+
916class BaseTestCase (unittest .TestCase ):
10- def setUp (self ):
11- self .pact = Consumer ('consumer' ).has_pact_with (
12- Provider ('provider' ))
17+ pass
1318
1419
1520class ExactMatches (BaseTestCase ):
1621 def test_get_user_sparse (self ):
1722 expected = {'name' : 'Jonas' }
18- (self . pact
23+ (pact
1924 .given ('a simple json blob exists' )
2025 .upon_receiving ('a request for a user' )
2126 .with_request ('get' , '/users/Jonas' )
2227 .will_respond_with (200 , body = expected ))
2328
24- with self . pact :
29+ with pact :
2530 result = requests .get ('http://localhost:1234/users/Jonas' )
2631
2732 self .assertEqual (result .json (), expected )
2833
2934 def test_post_user_complex (self ):
3035 expected = {'name' : 'Jonas' }
31- (self . pact
36+ (pact
3237 .given ('a simple json blob exists' )
3338 .upon_receiving ('a query for the user Jonas' )
3439 .with_request (
@@ -42,7 +47,7 @@ def test_post_user_complex(self):
4247 body = expected ,
4348 headers = {'Content-Type' : 'application/json' }))
4449
45- with self . pact :
50+ with pact :
4651 result = requests .post (
4752 'http://localhost:1234/users/?Jonas' ,
4853 headers = {'Accept' : 'application/json' },
@@ -53,21 +58,21 @@ def test_post_user_complex(self):
5358
5459class InexactMatches (BaseTestCase ):
5560 def test_sparse (self ):
56- (self . pact
61+ (pact
5762 .given ('the user `bob` exists' )
5863 .upon_receiving ('a request for the user object of `bob`' )
5964 .with_request ('get' , '/users/bob' )
6065 .will_respond_with (200 , body = {
6166 'username' : SomethingLike ('bob' ),
6267 'id' : Term ('\d+' , '123' )}))
6368
64- with self . pact :
69+ with pact :
6570 result = requests .get ('http://localhost:1234/users/bob' )
6671
6772 self .assertEqual (result .json (), {'username' : 'bob' , 'id' : '123' })
6873
6974 def test_nested (self ):
70- (self . pact
75+ (pact
7176 .given ('a list of users exists' )
7277 .upon_receiving ('a query of all users' )
7378 .with_request ('get' , '/users/' , query = {'limit' : Term ('\d+' , '5' )})
@@ -77,7 +82,7 @@ def test_nested(self):
7782 'groups' : EachLike (123 ),
7883 }, minimum = 2 )}))
7984
80- with self . pact :
85+ with pact :
8186 results = requests .get (
8287 'http://localhost:1234/users/?limit=4' )
8388
@@ -89,7 +94,7 @@ def test_nested(self):
8994
9095class SyntaxErrors (BaseTestCase ):
9196 def test_incorrect_number_of_arguments (self ):
92- (self . pact
97+ (pact
9398 .given ('a list of users exists' )
9499 .upon_receiving ('a request for a user' )
95100 .with_request ('get' , '/users/' )
@@ -99,7 +104,7 @@ def two(a, b):
99104 print ('Requires two arguments' )
100105
101106 with self .assertRaises (TypeError ) as e :
102- with self . pact :
107+ with pact :
103108 two ('one' )
104109
105110 self .assertEqual (
0 commit comments