Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
1
vote
1 answer

Python unittest, datetime

My problem is: In my following test, everything is working today but not will work tomorrow, I'm a beginner and did try a lot of options, but I failed, I,m trying pass "now" as a parameter but with no success until now. I have to stop the…
Flavio Oliveira
  • 381
  • 4
  • 12
1
vote
1 answer

Mock a Python class __init__ partially?

I have a Python class with complicated initialization. I would like to mock the class initialization to avoid writing too much scaffolding code. I want to test its non-mocked method. A simple example: class Person: def __init__(self, x, y, z): …
Alex Tereshenkov
  • 3,340
  • 8
  • 36
  • 61
1
vote
1 answer

Freeze Time test cases fail when a variable is used to reference the time

I'm using freeze-time to run my python unittest Test cases. A dummy test case: @freeze_time('2020-01-01') def test_something(self): expected_output = {'time': '2020-01-01'} output = call_tested_code() self.assertEqual(expected_output,…
piyush daga
  • 501
  • 4
  • 17
1
vote
0 answers

Testing creation of a global variable using globals() in python unittest

I am trying to save response from an HTTP request in a global variable using globals() in python so that I can use that data later. def request_agent(agent_name, urL): r = requests.get(urL) data = r.json() globals()[agent_name] =…
1
vote
1 answer

Django: All subsequent tests fail after one test fails

I've moved recently from Python 2.7 to Python 3.8. There's a strange new phenomenon when running the tests, that can be reproduced with this simple example: from django.test import TestCase from users.models import User class TestWTF(TestCase): …
emesik
  • 221
  • 1
  • 9
1
vote
1 answer

python unittest relative import

This question is related to Relative imports with unittest in Python but I would like to specifically ask about the importance of how unittest handles/modifies imports. Say I have a package structure: containing_folder/ project/ …
zephyrus
  • 1,266
  • 1
  • 12
  • 29
1
vote
3 answers

How to mock a method inside another method with Python unittest?

I am struggling to mock a method inside another method i.e Let's say I have 2 classes A, B as follows: class A: def method_A(self): return "Hi" class B: def method_B(self): instance_A = A() result =…
unitSphere
  • 241
  • 3
  • 17
1
vote
1 answer

Mock not overriding the return of a function in Python

I am implementing unit test on one of the classes of my project. The method that I want to test is queryCfsNoteVariations: class PdfRaportDaoImpl: def queryCfsNoteVariations(self, reportId): sql = """ select v.* from…
unitSphere
  • 241
  • 3
  • 17
1
vote
0 answers

why python is throwing attribute attribute error

I have the following unit test written in python,When i try to run this code, i am getting an attribute error in the console that AttributeError: 'testLogin' object has no attribute 'lp' I am very new to python and i would like to get some insight…
1
vote
1 answer

unittest.mock: Set custom attribute (variable) on specced mock object

I am trying to mock an existing object from another library's class for unit tests with pytest. However, the attributes (not methods) from the other library are mostly set during runtime. What I want to achieve Get all the benefits of mocking the…
1
vote
1 answer

Is there a way to pass in environment variables to python script?

I am trying to write a python test that involves testing if the environment variables provided are valid. I am passing the environment variables as follows env = EnvironmentVarGuard() env.set('GOOGLE_CREDENTIALS', GOOGLE_CREDENTIALS) The…
E_K
  • 2,159
  • 23
  • 39
1
vote
0 answers

best practice on Python unittest configuration?

My goal is to create a GitHub repo with a module and unit tests that other developers can check out and work on, including running the unit tests. This module does PDF manipulation, so I need to provide some sample PDFs to test, plus paths for…
1
vote
1 answer

Mocking method pandas.read_excel does not work with @patch

I'm writing unit tests with Python for a project and recently encountered a problem with the decorator @patch. I have the following method which I need to test def _read_from_disk(self, excel_kwargs): """ Read excel file from disk…
1
vote
1 answer

How to give sys.argv when using pytest and unittest.TestCase

I have a test file which run some binary executable and check the output. The test looks like this: #! /usr/bin/env python from subprocess import Popen, PIPE import unittest import sys class MyTest(unittest.TestCase): PATH = './app' def…
J_yang
  • 2,672
  • 8
  • 32
  • 61
1
vote
0 answers

VSCode test explorer stops discovering tests when I add an import to python code file

This python code file works perfectly. But when I add either of the commented imports, the vscode test feature gives "No tests discovered, please check the configuration settings for the tests." No other errors. # import boto3 # import…
jlembke
  • 13,217
  • 11
  • 42
  • 56