I am attempting an integration test using pytest for my application developed in python3.7 and asyncio. The application is supposed to connect to a remote server and if the network fails, my application should detect this and attempt to reconnect at a specified interval. Typically, in my integration test, I have my remote server already running and listening on a TCP port. My application should connect to that port and I will check that the connection was successful. Then I need to simulate a network outage in which application looses connection to the server and test the behaviour of the application while the network is not operational and then I need to bring the network back online and confirm that the app will properly reconnect and perform it's tasks. For the purposes of my integration testing all this stuff is running on my localhost.
Does pytest already have something for this usecase or should I build some sort of proxy server myself? How would I go about doing this?
Pytest doesn't have any feature to simulate network failure because it's just test runner.
You need to use external mock-server that can emulate connection failure or long response time. For this purpose I use and recommend mock-server Mountebank: http://www.mbtest.org/
With Mountebank you will be able to emulate any response from remote server. You can manage Mountebank behavior directly by API or use client libraries: http://www.mbtest.org/docs/clientLibraries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With