

If you would like to keep using unittest while using my library, I'm accepting pull requests. Mocker.patch(_name_ + '.ftplib', new=mock_ftplib) This time the test succeeds and I only need to collect the result of the second print to get the proper asserts: def test_download_file(mocker): I'm placing this in the test and would run it again: def test_download_file(mocker): So when I want client to enter passive mode, the client still thinks it is a tcp. this is the situation when I visit a formal FTP server. client still thinks I sent a TCP package. After creating the socket, a call is made to tsockopt () with the option socket.

but the client did not recognize the server sent a FTP package. Print(mock_asserts(mock_ftplib, name='mock_ftplib')) I want to use python socket to build a FTP server. Mock_ftplib = mocker.MagicMock(name='ftplib') When you run the test for the first time, it would fail due to unknown DNS, but the print statement which wraps my library would give us this valuable input. Name=_name_).mock_modules().prepare_asserts_calls().generate())
CREATE A SIMPLE FTP SERVER PYTHON CODE
You start by having your code and a basic pytest function, with the addition of my helper library to generate mocks to modules and the matching asserts generation: import ftplibįrom mock_autogen.pytest_mocker import PytestMocker I have went a bit further and have actually wrote a library which helps me to mock easily. Like Ibrohim's answer, I prefer pytest with mocker. How do I get the actual object the fuction is running? The long term goal is not having the "download_file" function in the same file, but calling it from a separate module file. I know it must be using the mock object since that is a fake server name, and when run without patching, it throws a "socket.gaierror" exception. When I run this, I get: AssertionError: Expected call: cwd('pub/files') Here is a simple example: import unittestĭef download_file(hostname, file_path, file_name):Ĭlass test_download_file(self, mock_ftp):ĭownload_file('', 'pub/files', 'wanted_file.txt') I've simplified everything down and still am not wrapping my head around how it works. I don't know why I'm just not getting this, but I want to use mock in Python to test that my functions are calling functions in ftplib.FTP correctly.
