只想写一个真正能用的django mock
Posted aguncn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了只想写一个真正能用的django mock相关的知识,希望对你有一定的参考价值。
调参数的过程,百转千回。
还好,搞得差不多了。
确实,方便写测试用例,
也是一个开发水平高低的衡量~~~:(
为了测试这个mock,不得不改下代码~~
还要不断的将Model里允许Null的参数写完,
不然,报错呀~~~
model_mommy,哟,不错的写测试数据的东东。
class DeployFunctionTests(TestCase): def setUp(self): self.user = User.objects.create_user( username=‘test‘, email=‘[email protected]‘, password=‘test‘,) self.client.login(username=‘test‘, password=‘test‘) self.new_server = mommy.make(Server, env_name__name=‘fat‘, app_name__script_url="http://", app_user=self.user, app_name__name="hello", app_name__zip_package_name="heh", app_name__package_name="heh", port="3456", ) self.new_deploy = mommy.make(DeployPool, name="2018--12-24-56XN", is_inc_tot=‘tot‘) DeployStatus.objects.create(name="FINISH", memo="FINISH") @patch(‘deploy.salt_cmd_views.cmd_run‘) def test_deploy_function(self, mock_cmd_run): mock_cmd_run.return_value = True self.assertEqual(deploy.salt_cmd_views.deploy( subserver_list=[[1, 2, 3], [4, 5, 6]], deploy_type="deployall", is_restart_server=True, user_name=self.user, deploy_version="2018--12-24-56XN", operation_type="deploy" ), True) @patch(‘deploy.salt_cmd_views.salt_run‘) def test_cmd_run_function(self, mock_salt_run): mock_salt_run.return_value = {‘return‘: [{self.new_server.name: {‘retcode‘: 0}}]} self.assertEqual(deploy.salt_cmd_views.cmd_run( server_id=self.new_server.id, action="deploy", user_name=self.user, percent_value="100%", deploy_version="2018--12-24-56XN", operation_type="deploy"), True)
以上是关于只想写一个真正能用的django mock的主要内容,如果未能解决你的问题,请参考以下文章