classTestUserService(BaseTestCase): """Tests for the Users Service."""
deftest_users(self): """Ensure the /ping route behaves correctly.""" response = self.client.get('/users/ping') data = json.loads(response.data.decode()) self.assertEqual(response.status_code, 200) self.assertIn('pong!', data['message']) self.assertIn('success', data['status'])
if __name__ == '__main__': unittest.main()
在 manage.py 中添加新的命令,以运行测试:
import unittest
# ...
@cli.command() deftest(): """ Runs the tests without code coverage""" tests = unittest.TestLoader().discover('project/tests', pattern='test*.py') result = unittest.TextTestRunner(verbosity=2).run(tests) if result.wasSuccessful(): return0 return1
# ...
由于添加了 flask-testing 包,我们需要重构镜像:
$ pip freeze > services/users/requirements.txt
$ docker-compose -f docker-compose-dev.yml up -d --build
容器启动后,运行测试:
$ docker-compose -f docker-compose-dev.yml run users python manage.py test
我们会看到如下报错:
self.assertTrue(app.config['SECRET_KEY'] == 'my_precious') AssertionError: False is not true