· 6 years ago · Mar 21, 2019, 02:56 AM
1# making test directory and adding files
2mkdir tests
3touch __init__.py
4touch base.py
5touch test_config.py
6touch test_users.py
7
8# add to base.py
9
10from flask_testing import TestCase
11
12from project import app, db
13
14
15class BaseTestCase(TestCase):
16 def create_app(self):
17 app.config.from_object('project.config.TestingConfig')
18 return app
19
20 def setUp(self):
21 db.create_all()
22 db.session.commit()
23
24 def tearDown(self):
25 db.session.remove()
26 db.drop_all()
27
28# add to test_config.py
29
30import os
31import unittest
32
33from flask import current_app
34from flask_testing import TestCase
35
36from project import app
37
38
39class TestDevelopmentConfig(TestCase):
40 def create_app(self):
41 app.config.from_object('project.config.DevelopmentConfig')
42 return app
43
44 def test_app_is_development(self):
45 self.assertTrue(app.config['SECRET_KEY'] == 'asdf')
46 self.assertFalse(current_app is None)
47 self.assertTrue(
48 app.config['SQLALCHEMY_DATABASE_URI'] ==
49 os.environ.get('DATABASE_URL')
50 )
51
52
53class TestTestingConfig(TestCase):
54 def create_app(self):
55 app.config.from_object('project.config.TestingConfig')
56 return app
57
58 def test_app_is_testing(self):
59 self.assertTrue(app.config['SECRET_KEY'] == 'asdf')
60 self.assertTrue(app.config['TESTING'])
61 self.assertFalse(app.config['PRESERVE_CONTEXT_ON_EXCEPTION'])
62 self.assertTrue(
63 app.config['SQLALCHEMY_DATABASE_URI'] ==
64 os.environ.get('DATABASE_TEST_URL')
65 )
66
67
68class TestProductionConfig(TestCase):
69 def create_app(self):
70 app.config.from_object('project.config.ProductionConfig')
71 return app
72
73 def test_app_is_production(self):
74 self.assertTrue(app.config['SECRET_KEY'] == 'asdf')
75 self.assertFalse(app.config['TESTING'])
76
77
78if __name__ == '__main__':
79 unittest.main()
80
81# add to test_users.py
82
83import json
84import unittest
85
86from project.tests.base import BaseTestCase
87
88
89class TestUserService(BaseTestCase):
90 """Tests for the Users Service."""
91
92 def test_users(self):
93 """Ensure the /ping route behaves correctly."""
94 response = self.client.get('/users/ping')
95 data = json.loads(response.data.decode())
96 self.assertEqual(response.status_code, 200)
97 self.assertIn('pong!', data['message'])
98 self.assertIn('success', data['status'])
99
100
101if __name__ == '__main__':
102 unittest.main()
103
104# add to requirements file in flask_web
105
106Flask-Testing==0.7.1
107
108
109# add to manage.py
110
111@cli.command()
112def test():
113 """Runs the tests without code coverage"""
114 tests = unittest.TestLoader().discover('project/tests', pattern='test*.py')
115 result = unittest.TextTestRunner(verbosity=2).run(tests)
116 if result.wasSuccessful():
117 return 0
118 return 1
119
120# important
121
122Import unittest
123
124# re-build images/run/update/retest
125
126docker-compose -f docker-compose-dev.yml up -d --build
127
128docker-compose -f docker-compose-dev.yml exec users python manage.py test
129
130class BaseConfig:
131 """Base configuration"""
132 TESTING = False
133 SQLALCHEMY_TRACK_MODIFICATIONS = False
134 SECRET_KEY = 'asdf' # new