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
Tag: unit-testing
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
Python Unittest and object initialization
My Python version is 3.5.1 I have a simple code (tests.py): If I run it with command ‘python tests.py’ I will get the results: Why it is happening? And how to fix it. I expect that each tests run will be independent (each test should pass), but it is not as we can see. Answer The array is shared by
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
Python unit test mock, get mocked function’s input arguments
I want a unit test to assert that a variable action within a function is getting set to its expected value, the only time this variable is used is when it is passed in a call to a library. My thought was that I could mock lib.event.Event, and get its input arguments and assert they are of specific value. >Is
InvalidBasesError: Cannot resolve bases for []
When I run tests I get this error during database initialization: I created this proxy for contrib.auth Group model to place it in my app in django admin: So what can I do to fix this issue? Answer After a lot of digging on this the only thing that worked for me was comment out the offending apps, run migrations,
How to properly assert that an exception gets raised in pytest?
Code: Output: How to make pytest print traceback, so I would see where in the whatever function an exception was raised? Answer pytest.raises(Exception) is what you need. Code Output Note that e_info saves the exception object so you can extract details from it. For example, if you want to check the exception call stack or another nested exception inside.
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
Django Test framework with file based Email backend server
I have formulated test cases in Django framework. Use Case: I am using API that register user by sending them an Email and when they click on the link provided in the Email their account get activated. In my settings.py I am using which points to the local directory. When running PyUnit test case from eclipse everything works file. Text