3,613 questions
1
vote
1
answer
103
views
How to test the calling sequence of methods of a class?
Sometimes when I develop by TDD (Test Driven Development) I need to test the calling order of some class methods. In general I write Python code so I'll show the last test case that I have just ...
-1
votes
1
answer
24
views
Python unittest.mock fails when using pydantic.BaseModel
I have come across this problem when making my unit tests.
from unittest.mock import AsyncMock, patch
import pydantic
# class Parent(): # Using non-pydantic class works
class Parent(pydantic....
0
votes
1
answer
70
views
Python unittest fails with No module loaded in VScode
I have the following directory structure
when I use the test runner I get
File "/home/kyle/dev/tests/test_foo.py", line 2, in <module> import foo ModuleNotFoundError: No module named ...
1
vote
1
answer
55
views
CheckConstraint in Django model not triggering in unittest.TestCase (AssertionError: IntegrityError not raised)
I have a Model class with a series of constraints that I am attempting to test, and I am unable to get these constraints to return an IntegrityError in testing. The class is as follows:
from django.db ...
1
vote
0
answers
54
views
Python: Running tests with all combinations of feature flags
We have several modules that require mandatory feature / backout flags. These flags are defined at module level.
module.py:
from enabled import is_enabled
FLAGS = {flag : is_enabled(flag) for flag in ...
-1
votes
1
answer
75
views
How to forcefully terminate a running Python test in VSCode
Closely related to my question is VSCode: how to interrupt a running Python test?, however in my case the standard method of pressing the square in the Test Results tap does not work.
What is ...
1
vote
0
answers
72
views
How to Test Password Reset Endpoint Using a Token That Doesn’t Exist in the Database in FastAPI?
I'm building a backend system using FastAPI, and I'm currently working on implementing unit tests for the password reset functionality that involves using tokens.
Here’s the snippet of the code I'm ...
0
votes
1
answer
69
views
Difference between mock.patch.dict as function decorator and context manager in Python unittest
I have a configuration module, config with data config.data where I want to add a value for testing.
Ideally, I want to use mock.patch.dict as a context manager because the value is a class attribute ...
4
votes
1
answer
367
views
RuntimeError: Event loop is closed - unittest, pytest
I have been working on yet another API using FastAPI and trying to write test cases for the APIs, but facing error that event loop is closed.
My setup:
So, I am using asyncpg driver/library to connect ...
1
vote
0
answers
78
views
importlib resources files -> not a package, empty module
I tend to alwasy run the testers from inside the module folder, but this breaks the "resources.files" functionality, as it seems not to be able to find the module any more
module folder
...
1
vote
2
answers
47
views
How to combine unittest with trace module to produce coverage like report?
I know how to use coverage module to get "human readable" coverage output (using popular third party coverage module). This can be used with pytest or unittest (with some differences in the ...
1
vote
1
answer
62
views
How can I apply side effects to a function of an instance that is created in a function that is tested?
I have a custom class in the file MyClass.py
class MyClass:
def __init__(self):
self.fetched_data = False
def get_data(self):
self.fetched_data = True
...
0
votes
2
answers
58
views
Running Django tests ends with MigrationSchemaMissing exception
I'm writing because I have a big problem. Well, I have a project in Django where I am using django-tenants. Unfortunately, I can't run any tests as these end up with the following error when calling ...
1
vote
1
answer
167
views
Mock asyncio.sleep to be faster in unittest
I want to mock asyncio.sleep to shorten the delay, e.g. by a factor of 10, to speed up my tests while also trying to surface any possible race conditions or other bugs as a crude sanity check. However ...
2
votes
1
answer
42
views
Test that unittest.Mock was called with some specified and some unspecified arguments [duplicate]
We can check if a unittest.mock.Mock has any call with some specified arguments.
I now want to test that some of the arguments are correct, while I do not know about the other ones.
Is there some ...