I have a Python code to read some files from FPT using FTPS and TLS version 1.2, this is the function, the credentials are read from AWS secret manager:
def ftp_connection(host, username, password):
try:
ftp_connection = ftplib.FTP_TLS(host, username, password)
# set TLS version 1.2
ftp_connection.ssl_version = ssl.PROTOCOL_TLSv1_2
# switching to secure data connection
ftp_connection.prot_p()
except ftplib.all_errors as err:
print(err)
return ftp_connection
I wonder how I can write unit tests for this function, I'd like to test: 1.verify TLS v1.2 connection 2. verify it's using the secure data connection
I'm new to unit tests, anything else that can be added to the unit tests? Many thanks.
This is what I have tried by following the first answer in this page:
@patch('ftplib.FTP_TLS', autospec=True)
def test_open_ftp_connection(self, mock_ftp_constructor):
mock_ftp = mock_ftp_constructor.return_value
read_news_ftp_read.open_ftp_connection('test_host', 'test_username', 'test_password')
mock_ftp_constructor.assert_called_with('test_host', 'test_username', 'test_password')
self.assertTrue(mock_ftp.login.called)
This gave me error:
File "C:\User\AppData\Local\Continuum\anaconda3\envs\Env-python3.7\lib\unittest\case.py", line 692, in assertTrue
raise self.failureException(msg)
AssertionError: False is not true
Assertion failed
ftplib
, you also don't test the FTP connection. You are just testing if you are using theftplib
as it should be used. Useunittest.mock
to replaceftplib.FTP_TLS
with aMock
and test if the mock is used as planned. – Couponmock
but got an error, I've updated my question, could you take a look for me please? Many thanks. – Fetterlockmock_ftp_constructor.host
. – Coupon