I could use some much needed guidance on unit tests for python3
All my files are in the same directory. No Sub folders.
Params is a class that gets initialized in the script. FILE 1 works fine connecting to the API.
#FILE 1 AssetsSensu.py
import requests
import json
from Params import params
class Assets():
def GetAssets():
response = requests.request(
"GET", params.url+"assets", headers=params.headers, verify=params.verify)
return response
#FILE 2 AssetsSensu_tests.py
from Params import params
from AssetsSensu import Assets
from unittest import TestCase, mock
class TestAPI(TestCase):
@mock.patch('AssetsSensu.Assets.GetAssets', return_val=200)
def test_GetAssets(self, getassets):
self.result = Assets.GetAssets().status_code
self.assertEquals(self.result, 200)
I am getting this result, running Assets.GetAssets().status_code returns 200 in either file.
AssertionError: <MagicMock name='GetAssets().status_code' id='140685448294016'> != 200
Inside File 2 "getassets" is not highlighted which means to me it's not being used. If I remove it I get the following error
TypeError: test_GetAssets() takes 1 positional argument but 2 were given
command being used is python3 -m unittest AssetsSensu_tests.py
Help would be greatly appreciated.
You are testing the
GetAssets
method so you should NOT mock it. Instead, you should mockrequests.request
method and its return value. We userequests.Response()
class to create a mocked response.E.g. (Python 3.9.6, requests==2.26.0)
AssetsSensu.py
:Params.py
:AssetsSensu_tests.py
:Test result: