Skip to content

Commit d0e18c2

Browse files
author
Jon Wayne Parrott
committed
Merge pull request #189 from GoogleCloudPlatform/pytest-mvms
Moving mvm samples to use py.test
2 parents 95d112e + 8a46667 commit d0e18c2

File tree

10 files changed

+135
-158
lines changed

10 files changed

+135
-158
lines changed

managed_vms/cloudsql/main_test.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@
1313
# limitations under the License.
1414

1515
import main
16-
from testing import CloudTest
1716

1817

19-
class CloudSqlTest(CloudTest):
18+
def test_index():
19+
main.db.create_all()
2020

21-
def test_index(self):
22-
main.db.create_all()
21+
main.app.testing = True
22+
client = main.app.test_client()
2323

24-
main.app.testing = True
25-
client = main.app.test_client()
26-
27-
r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
28-
self.assertEqual(r.status_code, 200)
29-
self.assertTrue('127.0' in r.data.decode('utf-8'))
24+
r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
25+
assert r.status_code == 200
26+
assert '127.0' in r.data.decode('utf-8')

managed_vms/datastore/main_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
# limitations under the License.
1414

1515
import main
16-
from testing import CloudTest
1716

1817

19-
class DatastoreTest(CloudTest):
18+
def test_index():
19+
main.app.testing = True
20+
client = main.app.test_client()
2021

21-
def test_index(self):
22-
main.app.testing = True
23-
client = main.app.test_client()
24-
25-
r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
26-
self.assertEqual(r.status_code, 200)
27-
self.assertTrue('127.0' in r.data.decode('utf-8'))
22+
r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
23+
assert r.status_code == 200
24+
assert '127.0' in r.data.decode('utf-8')

managed_vms/disk/main_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
# limitations under the License.
1414

1515
import main
16-
from testing import CloudTest
1716

1817

19-
class DiskTest(CloudTest):
18+
def test_index():
19+
main.app.testing = True
20+
client = main.app.test_client()
2021

21-
def test_index(self):
22-
main.app.testing = True
23-
client = main.app.test_client()
24-
25-
r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
26-
self.assertEqual(r.status_code, 200)
27-
self.assertTrue('127.0' in r.data.decode('utf-8'))
22+
r = client.get('/', environ_base={'REMOTE_ADDR': '127.0.0.1'})
23+
assert r.status_code == 200
24+
assert '127.0' in r.data.decode('utf-8')

managed_vms/extending_runtime/main_test.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,16 @@
1515
import os
1616

1717
import main
18-
from nose.plugins.skip import SkipTest
19-
from testing import CloudTest
18+
import pytest
2019

2120

22-
class ExtendingRuntimeTest(CloudTest):
21+
@pytest.mark.skipif(
22+
not os.path.exists('/usr/games/fortune'),
23+
reason='Fortune executable is not installed.')
24+
def test_index():
25+
main.app.testing = True
26+
client = main.app.test_client()
2327

24-
def test_index(self):
25-
if not os.path.exists('/usr/games/fortune'):
26-
raise SkipTest(
27-
'Extending runtime test will only execute if fortune is'
28-
'installed')
29-
30-
main.app.testing = True
31-
client = main.app.test_client()
32-
33-
r = client.get('/')
34-
self.assertEqual(r.status_code, 200)
35-
self.assertTrue(len(r.data))
28+
r = client.get('/')
29+
assert r.status_code == 200
30+
assert len(r.data)

managed_vms/hello_world/main_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
# limitations under the License.
1414

1515
import main
16-
from testing import CloudTest
1716

1817

19-
class HelloWorldTest(CloudTest):
18+
def test_index():
19+
main.app.testing = True
20+
client = main.app.test_client()
2021

21-
def test_index(self):
22-
main.app.testing = True
23-
client = main.app.test_client()
24-
25-
r = client.get('/')
26-
self.assertEqual(r.status_code, 200)
27-
self.assertTrue('Hello World' in r.data.decode('utf-8'))
22+
r = client.get('/')
23+
assert r.status_code == 200
24+
assert 'Hello World' in r.data.decode('utf-8')

managed_vms/hello_world_compat/main_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
# limitations under the License.
1414

1515
import main
16-
from testing import CloudTest
1716

1817

19-
class HelloWorldTest(CloudTest):
18+
def test_index():
19+
main.app.testing = True
20+
client = main.app.test_client()
2021

21-
def test_index(self):
22-
main.app.testing = True
23-
client = main.app.test_client()
24-
25-
r = client.get('/')
26-
self.assertEqual(r.status_code, 200)
27-
self.assertTrue('Hello World' in r.data.decode('utf-8'))
22+
r = client.get('/')
23+
assert r.status_code == 200
24+
assert 'Hello World' in r.data.decode('utf-8')

managed_vms/memcache/main_test.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,23 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from unittest.case import SkipTest
16-
1715
import main
18-
from testing import CloudTest
19-
16+
import pytest
2017

21-
class MemcacheTest(CloudTest):
2218

23-
def test_index(self):
24-
main.memcache_client.set('counter', 0)
19+
def test_index():
20+
main.memcache_client.set('counter', 0)
2521

26-
if main.memcache_client.get('counter') is None:
27-
raise SkipTest('Memcache is unavailable.')
22+
if main.memcache_client.get('counter') is None:
23+
pytest.skip('Memcache is unavailable.')
2824

29-
main.app.testing = True
30-
client = main.app.test_client()
25+
main.app.testing = True
26+
client = main.app.test_client()
3127

32-
r = client.get('/')
33-
self.assertEqual(r.status_code, 200)
34-
self.assertTrue('1' in r.data.decode('utf-8'))
28+
r = client.get('/')
29+
assert r.status_code == 200
30+
assert '1' in r.data.decode('utf-8')
3531

36-
r = client.get('/')
37-
self.assertEqual(r.status_code, 200)
38-
self.assertTrue('2' in r.data.decode('utf-8'))
32+
r = client.get('/')
33+
assert r.status_code == 200
34+
assert '2' in r.data.decode('utf-8')

managed_vms/pubsub/main_test.py

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,52 @@
1717
import os
1818

1919
import main
20-
from testing import CloudTest
21-
22-
23-
class PubSubTest(CloudTest):
24-
def setUp(self):
25-
super(PubSubTest, self).setUp()
26-
main.app.testing = True
27-
self.client = main.app.test_client()
28-
29-
def test_index(self):
30-
r = self.client.get('/')
31-
self.assertEqual(r.status_code, 200)
32-
33-
def test_post_index(self):
34-
r = self.client.post('/', data={'payload': 'Test payload'})
35-
self.assertEqual(r.status_code, 200)
36-
37-
def test_push_endpoint(self):
38-
url = '/pubsub/push?token=' + os.environ['PUBSUB_VERIFICATION_TOKEN']
39-
40-
r = self.client.post(
41-
url,
42-
data=json.dumps({
43-
"message": {
44-
"data": base64.b64encode(
45-
u'Test message'.encode('utf-8')
46-
).decode('utf-8')
47-
}
48-
})
49-
)
50-
51-
self.assertEqual(r.status_code, 200)
52-
53-
# Make sure the message is visible on the home page.
54-
r = self.client.get('/')
55-
self.assertEqual(r.status_code, 200)
56-
self.assertTrue('Test message' in r.data.decode('utf-8'))
57-
58-
def test_push_endpoint_errors(self):
59-
# no token
60-
r = self.client.post('/pubsub/push')
61-
self.assertEqual(r.status_code, 400)
62-
63-
# invalid token
64-
r = self.client.post('/pubsub/push?token=bad')
65-
self.assertEqual(r.status_code, 400)
20+
import pytest
21+
22+
23+
@pytest.fixture
24+
def client():
25+
main.app.testing = True
26+
return main.app.test_client()
27+
28+
29+
def test_index(client):
30+
r = client.get('/')
31+
assert r.status_code == 200
32+
33+
34+
def test_post_index(client):
35+
r = client.post('/', data={'payload': 'Test payload'})
36+
assert r.status_code == 200
37+
38+
39+
def test_push_endpoint(client):
40+
url = '/pubsub/push?token=' + os.environ['PUBSUB_VERIFICATION_TOKEN']
41+
42+
r = client.post(
43+
url,
44+
data=json.dumps({
45+
"message": {
46+
"data": base64.b64encode(
47+
u'Test message'.encode('utf-8')
48+
).decode('utf-8')
49+
}
50+
})
51+
)
52+
53+
assert r.status_code == 200
54+
55+
# Make sure the message is visible on the home page.
56+
r = client.get('/')
57+
assert r.status_code == 200
58+
assert 'Test message' in r.data.decode('utf-8')
59+
60+
61+
def test_push_endpoint_errors(client):
62+
# no token
63+
r = client.post('/pubsub/push')
64+
assert r.status_code == 400
65+
66+
# invalid token
67+
r = client.post('/pubsub/push?token=bad')
68+
assert r.status_code == 400

managed_vms/static_files/main_test.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@
1313
# limitations under the License.
1414

1515
import main
16-
from testing import CloudTest
1716

1817

19-
class StaticFilesTest(CloudTest):
18+
def test_index():
19+
main.app.testing = True
20+
client = main.app.test_client()
2021

21-
def test_index(self):
22-
main.app.testing = True
23-
client = main.app.test_client()
22+
r = client.get('/')
23+
assert r.status_code == 200
2424

25-
r = client.get('/')
26-
self.assertEqual(r.status_code, 200)
27-
28-
r = client.get('/static/main.css')
29-
self.assertEqual(r.status_code, 200)
25+
r = client.get('/static/main.css')
26+
assert r.status_code == 200

managed_vms/storage/main_test.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,37 @@
1313
# limitations under the License.
1414

1515
import main
16+
import pytest
1617
import requests
1718
from six import BytesIO
18-
from testing import CloudTest
1919

2020

21-
class StorageTest(CloudTest):
22-
def setUp(self):
23-
super(StorageTest, self).setUp()
24-
main.app.testing = True
25-
self.client = main.app.test_client()
21+
@pytest.fixture
22+
def client():
23+
main.app.testing = True
24+
return main.app.test_client()
2625

27-
def test_index(self):
28-
r = self.client.get('/')
29-
self.assertEqual(r.status_code, 200)
3026

31-
def test_upload(self):
32-
# Upload a simple file
33-
file_content = b"This is some test content."
27+
def test_index(client):
28+
r = client.get('/')
29+
assert r.status_code == 200
3430

35-
r = self.client.post(
36-
'/upload',
37-
data={
38-
'file': (BytesIO(file_content), 'example.txt')
39-
}
40-
)
4131

42-
self.assertEqual(r.status_code, 200)
32+
def test_upload(client):
33+
# Upload a simple file
34+
file_content = b"This is some test content."
4335

44-
# The app should return the public cloud storage URL for the uploaded
45-
# file. Download and verify it.
46-
cloud_storage_url = r.data.decode('utf-8')
47-
r = requests.get(cloud_storage_url)
48-
self.assertEqual(r.text.encode('utf-8'), file_content)
36+
r = client.post(
37+
'/upload',
38+
data={
39+
'file': (BytesIO(file_content), 'example.txt')
40+
}
41+
)
42+
43+
assert r.status_code == 200
44+
45+
# The app should return the public cloud storage URL for the uploaded
46+
# file. Download and verify it.
47+
cloud_storage_url = r.data.decode('utf-8')
48+
r = requests.get(cloud_storage_url)
49+
assert r.text.encode('utf-8') == file_content

0 commit comments

Comments
 (0)