Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
1
vote
0 answers

Get and Post request is not working in unittests

I have a problem writing tests for my API routes. I can GET and POST data via Postman or via browser but when I do a POST, GET request from the testing environment it always returns 404 response. I tried different routes none of them works. What…
Andrew
  • 1,507
  • 1
  • 22
  • 42
1
vote
0 answers

How to classically remove a TemporaryDirectory (without `with` statement)

Tempfile is normaly used in combination with the with statement. I would like to use it without it in a unittest class. The tests create a number of files which do not interfer with each other. Thus, I planned to just create a temp dir in which I…
Molitoris
  • 935
  • 1
  • 9
  • 31
1
vote
0 answers

VScode python unittest: no tests discovered

I can't seem to get vscode to detect tests for a simple project: Folder structure: ~/Documents/My_proj$ tree . ├── tests │ ├── __init__.py │ └── test_main.py └── xyz ├── __init__.py ├── main.py ├── subpkg1 │ ├── __init__.py …
Eric L
  • 592
  • 1
  • 6
  • 20
1
vote
0 answers

How to Mock self variable created in method

I got stuck while writing unit test cases by mock function. I have a self variable created inside of the function, not by the init method. When I run the script, it throws an error. myFooFile.py: class Foo(object): def my_foo(self): …
kumar
  • 41
  • 7
1
vote
1 answer

mocking objects within a decorator while using pytest

In the below example , I have a decorator. In the decorator , I am instantiating a DB connection class. I have a test class below , where I want to mock the DB connection class within the decorator. How can I do that? # importing libraries import…
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
1
vote
3 answers

unittest: NameError: name 'dbutils' is not defined

How do you unit test python files from a Databricks .ipynb without importing everything from the .ipynb file? For example, I'm attempting to use unittest from VS Code on my desktop, where I've cloned the .ipynb file from my Azure Databricks…
Dave Voyles
  • 4,495
  • 7
  • 33
  • 44
1
vote
0 answers

Secret Key not found when Django Unit Tests are executed

Whenever I'm running my Django-Tests via the unittests.py file in the main folder the SECRET KEY in .env is not found. What is the correct way to set up the regular Unittests-File for a series of Django-Tests? I wrote my settings.py in a way it's…
Qohelet
  • 1,459
  • 4
  • 24
  • 41
1
vote
1 answer

Django Rest Framework unit testing for PUT request

"test_put_method_success" is showing AssertionError: 404 != 200. How to solve it? ...................... class BasicTest(APITestCase): def setUp(self): self.client = Client() self.user = User(username="admin",…
1
vote
1 answer

How to go about converting a test-suite from unittest to pytest?

I know that one can run the tests written for unittest directly by pytest. But I want to rewrite them to make use of all that pytest has to offer. So, I would like to know How to go about doing it manually? If there is an automated tool which can…
Tejas Shetty
  • 685
  • 6
  • 30
1
vote
1 answer

Correct usage of assertRaises in Python unit tests

I'm trying to figure out how to correctly use assertRaises() in Python unit testing. I have a ValueError in my function that gets raised. However, when testing in my unit tests, I'm getting mixed results with two different ways of using…
mlenthusiast
  • 1,094
  • 1
  • 12
  • 34
1
vote
1 answer

Testing Async Coroutines With Context Manager

I have been trying to test a Context managed Async coroutine through aiobotocore in Python 3.7 . I have been using the asynctest package to get the included MagicMock which has the magic methods of __aenter__ and __aexit__ and a custom mock factory…
JSwordy
  • 169
  • 1
  • 2
  • 13
1
vote
0 answers

Using global variables in django testing is a best practice..?

In django testing I have a Imagefield which is used in many test cases, In order to avoid the repetition I have assigned the Imagefile obj to the global variable file = open(os.path.join(settings.BASE_DIR, 'logged_out.jpg'), 'rb') image =…
ivardu
  • 179
  • 2
  • 15
1
vote
1 answer

Python Unittest woes: not all argument converted during string formatting

When running unittest, I receive a "not all arguments converted during string formatting." When running the code directly, everything works fine; it's only running Unittest that's a problem. When I run the same exact command…
verblox
  • 53
  • 6
1
vote
2 answers

Python unittest -- using parent class methods in my setUpClass method?

I am writing unit tests and have the following structure: class TestSuite(unittest.TestCase, HelperClass): @classmethod def setUpClass(cls): # I want to use methods from HelperClass here, but get errors # I have tried cls.method_name()…
Danny
  • 470
  • 1
  • 4
  • 21
1
vote
1 answer

ERROR: https://www (unittest.loader._FailedTest) while Unit Testing

Currently trying to write a unittest for a particular function. The error is shown below: E ====================================================================== ERROR: https://www…
SDG
  • 2,260
  • 8
  • 35
  • 77
1 2 3
99
100