Mock Python’s built in print function

I know that there is already an accepted answer but there is simpler solution for that problem – mocking the print in python 2.x. Answer is in the mock library tutorial: http://www.voidspace.org.uk/python/mock/patch.html and it is: >>> from StringIO import StringIO >>> def foo(): … print ‘Something’ … >>> @patch(‘sys.stdout’, new_callable=StringIO) … def test(mock_stdout): … foo() … Read more

What is your favorite Python mocking library? [closed]

I’ve only used one, but I’ve had good results with Michael Foord’s Mock: http://www.voidspace.org.uk/python/mock/. Michael’s introduction says it better than I could: There are already several Python mocking libraries available, so why another one? Most mocking libraries follow the ‘record -> replay’ pattern of mocking. I prefer the ‘action -> assertion’ pattern, which is more … Read more

Mocking boto3 S3 client method Python

Botocore has a client stubber you can use for just this purpose: docs. Here’s an example of putting an error in: import boto3 from botocore.stub import Stubber client = boto3.client(‘s3’) stubber = Stubber(client) stubber.add_client_error(‘upload_part_copy’) stubber.activate() # Will raise a ClientError client.upload_part_copy() Here’s an example of putting a normal response in. Additionally, the stubber can now … Read more

Moq: unit testing a method relying on HttpContext

Webforms is notoriously untestable for this exact reason – a lot of code can rely on static classes in the asp.net pipeline. In order to test this with Moq, you need to refactor your GetSecurityContextUserName() method to use dependency injection with an HttpContextBase object. HttpContextWrapper resides in System.Web.Abstractions, which ships with .Net 3.5. It is … Read more

How to monkeypatch python’s datetime.datetime.now with py.test?

You need to monkeypatch datetime.now function. In example below, I’m creating fixture which I can re-use later in other tests: import datetime import pytest FAKE_TIME = datetime.datetime(2020, 12, 25, 17, 5, 55) @pytest.fixture def patch_datetime_now(monkeypatch): class mydatetime: @classmethod def now(cls): return FAKE_TIME monkeypatch.setattr(datetime, ‘datetime’, mydatetime) def test_patch_datetime(patch_datetime_now): assert datetime.datetime.now() == FAKE_TIME

What is the best way to unit-test SLF4J log messages?

Create a test rule: import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; import org.slf4j.LoggerFactory; import java.util.List; import java.util.stream.Collectors; public class LoggerRule implements TestRule { private final ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); private final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); @Override public Statement apply(Statement base, Description description) { return new Statement() { @Override … Read more