Python Forum
unittest.mock for an api key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unittest.mock for an api key
#1
I have a method call getKey() returning url and key and I need to test the function using python unittest.mock

def getKey():

     key = "xxxxx"
     url = 'www.test.com'

     return url,key
Yoriz write Aug-25-2022, 05:53 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Can you explain further? What do you mean when you say you "need to test the function using unittest.mock"? Is the function really as simple as the code you've shown? Do you really mean you're testing something else and you need to provide a mocked implementation of this function?

Can you show the kinds of tests you're trying to write?

At this point, it's really not clear what you're trying to do.
Reply
#3
I need to create a def test for that method with a mock data. that's all
Yoriz write Aug-29-2022, 04:43 PM:
Removed incorrect quote of @ndc85430
Reply
#4
I didn't write that; you need to fix that post.

What does the function do? Is it getting the data from somewhere? Where? If the values are just hardcoded in the test, what value is there in testing it? If you're going to just mock the function, you're not really testing it, either. Of course, the other thing is that you shouldn't put your secret values like API keys in code anyway, as you'll end up storing them in version control and anyone with access to the code will have them (e.g. if the repo is compromised).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using unittest akbarza 2 330 Feb-25-2024, 12:51 PM
Last Post: deanhystad
Question Unwanted execution of unittest ThomasFab 9 2,073 Nov-15-2022, 05:33 PM
Last Post: snippsat
  Mock obj - How to call the side_effect every time during the run? pythonisbae 3 2,650 Mar-06-2022, 09:37 AM
Last Post: pythonisbae
  Ran 0 tests in 0.000s - unittest Peaches 8 5,124 Dec-31-2021, 08:58 AM
Last Post: Peaches
Sad Problem with Unittest mhanusek 1 3,769 Nov-12-2020, 04:58 PM
Last Post: Gribouillis
  Unittest et patch mad31 2 2,139 Aug-09-2020, 06:16 AM
Last Post: mad31
  Unusual things to do with unittest/HTMLTestRunner AndyHolyer 0 2,148 Jul-29-2020, 02:43 PM
Last Post: AndyHolyer
  Test a class function via "unittest " Penguin827 1 1,625 Jul-10-2020, 08:31 AM
Last Post: Gribouillis
  StopIteration exception when mock PostgreSQL connection in several tests igor87z 1 2,930 Jun-10-2020, 06:16 PM
Last Post: ibreeden
  How to use unittest module ? binhduonggttn 1 2,053 Feb-17-2020, 03:28 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020