Questions tagged [python-unittest]
Python's standard library framework for testing.
python-unittest
3,589
questions
0
votes
1
answer
26
views
Patching in helper function not appled to instance
I am creating a test with unittest.mock.patch that looks something like this:
class TestService:
def test_patched(self):
service = Service()
with patch.object(service, "...
-2
votes
0
answers
39
views
Python can't find module in same FILE?
I am working with Python unit tests and am trying to create a basic test.
In doing so, I have created a 'Person' class and am attempting to mock one of its methods. However, when I run the file, ...
0
votes
0
answers
21
views
Error while running coverage run because of os.environ in python
My code is importing several variables using os.environ. When I run 'coverage run' command for testing, it gives key error. Is there a way to provide env files to read env variables from there, or ...
1
vote
1
answer
108
views
How should I deal with multiple imports having the same name
I have a repository that contains multiple programs:
.
└── Programs
├── program1
│ └── Generic_named.py
└── program2
└── Generic_named.py
I would like to add testing to this ...
0
votes
1
answer
26
views
mocking a self-defined decorator which has a function in it
I am trying to unit test a flask application route that calls a function that is wrapped by a self-defined decorator called verify_jwt.
This decorator is defined in the same module where are defined ...
1
vote
1
answer
60
views
How to mock sqlalchemy's async session?
Code snippet
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
engine = create_async_engine(url="")
session_maker = async_sessionmaker(engine)
How can I mock the ...
3
votes
1
answer
53
views
TextTestRunner doesn't recognize modules when executing tests in a different project
i am currently working on a project, where i need to run tests inside a different file structure like this:
/my_project
├── __init__.py
├── ...my python code
/given_proj
├── __init__.py
├── /package
│ ...
3
votes
1
answer
44
views
How to mock imported function?
I've tried multiple ways and just can't get the result I'm looking for. It seems like a lot of questions use a different type of mocking with the whole @patch decorators but I've been using this more ...
0
votes
0
answers
32
views
pytest - argument missing when using parametrize
I have a list of dictionaries non_metric_cases which are the test cases i want to run using the below code:
class TestEntityExtractionNonMetricFlow(unittest.IsolatedAsyncioTestCase):
async def ...
0
votes
0
answers
20
views
AttributeError when running unittest with coverage in Python 2.7 on WSL
I'm encountering an error while attempting to run unit tests with unittest and coverage in Python 2.7 on a Windows 11 system using WSL (Windows Subsystem for Linux). The error message suggests an ...
2
votes
1
answer
72
views
Is there a way to mock .strip() for unit testing in Python 2.7's unittest module?
I am using Python 2.7 for a coding project. I'd love to switch to Python 3, but unfortunately I'm writing scripts for a program that only has a python package in 2.7 and even if it had one in 3 our ...
0
votes
1
answer
44
views
How to import and test the target function only without touching other codes in the file?
I'm trying to perform a unit test on each function of a Python file using unittest.
Assuming the file I want to test is called X.py and the file containing the testing code is Y.py.
In file X, there ...
5
votes
3
answers
90
views
Why is format() throwing ValueError: Unknown format code 'f' for object of type 'str' when I'm not inputting a string?
I am using Python 2.7. (Switching to Python 3 for this particular code is not an option, please don't suggest it.) I am writing unit tests for some code.
Here is the relevant piece of code:
class ...
0
votes
0
answers
28
views
Handling very long import path
I need to patch a test method and have a path that so long that it goes above E501 80 symbols limit. Change of package name is not possible due dependencies elsewhere.
A sample of the problem:
from ...
1
vote
1
answer
46
views
Using unittest with pandas
I am new to unit tests in general and Python's unittest in particular.
When trying to validate a pandas dataframe df, I typically:
Check whether df is empty (using one of the methods detailed here).
...