Tuesday, 15 March 2011

python - How to patch a function that a Flask view calls -


Is

Want to send a message to AWS Skyues with my Vebpop Bobo and I shows the need to send the actual message that calling that Send_message is called though I do not understand how to use a function that fake python to patch the call being a function being tested.

Can I get the joke out of Boto con.send_message as code like proxy below?

views.py:

  @ app.route ( '/ test') def send_msg (): con = boto.sqs.connect_to_region ( "eU West -1" , aws_access_key_id = "asd", aws_secret_access_key = "asd") que = con.get_queue { 'data': 'asd'} ( 'my_queue') msg ​​= json.dumps () R = con.send_message (Q, msg )  

tests.py

  class MyTestCase (unittest.TestCase): def test_test (self): patch ( 'views.con.send_message') sqs_send As with: text "itemprop =" after itself: .test_client.get ('/ test') loud (sqs_send.called)  

.

The case of your trial can be something like this:

  class MyTestCase (unittest.TestCase): @patch ( "boto.sqs.connect_to_region", autospec = true) def test_test (self, mock_connect_to_region): Understanding mocked by connect_to_region mock_con = mock_connect_to_region connection #grab. #call customer self.test_client.get ( '/ test') #test connect_to_region call mock_connect_to_region.assert_called_with ( "eU West -1", aws_access_key_id = "asd", aws_secret_access_key = "asd") #test get_queue () mock_con return_value. Get_queue.assert_called_with ('my_queue') #finaly test send_message mock_con.send_message.assert_called_with (mock_con.get_queue.return_value, json.dumps ({'data': 'asd'}))  

Just a few notes:

  1. I wrote it in a white box style and chi all the calls in your view CQ: you can loose it more and some check can leave; Use self.assertTrue (mock_con.send_message.called) You just want to check or use the call mock.ANY as argument if you have some logic in the content Are not interested.
  2. autospec = true is not mandatory, but very useful: take a look at
  3. I'm sorry that there is some error in the code ... I can not test it anymore but I hope this idea is quite clear.

No comments:

Post a Comment