Skip to content
Advertisement

Tag: unit-testing

Mock an entire module in python

I have an application that imports a module from PyPI. I want to write unittests for that application’s source code, but I do not want to use the module from PyPI in those tests. I want to mock it entirely (the testing machine will not contain that PyPI module, so any import will fail). Currently, each time I try to

Python: how to mock a kafka topic for unit tests?

We have a message scheduler that generates a hash-key from the message attributes before placing it on a Kafka topic queue with the key. This is done for de-duplication purposes. However, I am not sure how I could possibly test this deduplication without actually setting up a local cluster and checking that it is performing as expected. Searching online for

How to mock imported library methods with unittest.mock in Python 3.5?

Is it possible to mock methods of imported modules with unittest.mock in Python 3.5? I expected that the the os.listdir method returns the specified side_effect “a” on the first call, but inside of my_function the unpatched os.listdir is called. Answer unittest.mock have two main duties: Define Mock objects: object designed to follow your screenplay and record every access to your

Specify specific testcases in Python unit test TestLoader

I have the following folder structure. Both the test files have two test cases each. File smoke.py contains The above code runs four test cases which is expected. Is there a way to run some specific test cases from file test1.py and test2.py where I can explicitly add those testcases to the suite1 and suite 2 in the above code.

How to monkeypatch builtin function datetime.datetime.now?

I’d like to make sure that datetime.datetime.now() returns a specific datetime for testing purposes, How do I do this? I’ve tried with pytest’s monkeypatch But this gives me the error TypeError: can’t set attributes of built-in/extension type ‘datetime.datetime’ Answer As the error tells you, you can’t monkeypatch the attributes of many extension types implemented in C. (Other Python implementations may

Advertisement