Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [python-unittest]

Python's standard library framework for testing.

python-unittest
1 vote
2 answers
55 views

Issues Reading a File when Using unittest in Python

I am unit testing a credit card validating program that accepts a .txt file containing fake credit cards numbers, and returns a dictionary of the card, whether it's a valid credit card number, and the ...
Camocarrot's user avatar
0 votes
1 answer
28 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, "...
GSP's user avatar
  • 3,777
-2 votes
0 answers
42 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, ...
Matthew's user avatar
  • 853
0 votes
0 answers
22 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 ...
Rishi's user avatar
  • 124
1 vote
1 answer
109 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 ...
Programmer S's user avatar
0 votes
1 answer
28 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 ...
mokhinurra's user avatar
1 vote
2 answers
74 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 ...
Michael Xia's user avatar
3 votes
1 answer
57 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 │ ...
jonas's user avatar
  • 304
3 votes
1 answer
46 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 ...
Darius Fiallo's user avatar
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 ...
Prerit Garg's user avatar
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 ...
a_retr0's user avatar
2 votes
1 answer
74 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 ...
Réka's user avatar
  • 133
0 votes
1 answer
46 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 ...
Yichen Zhang's user avatar
5 votes
3 answers
98 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 ...
Réka's user avatar
  • 133
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 ...
dmayilyan's user avatar

15 30 50 per page
1
2 3 4 5
240