Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
1
vote
0 answers

Python unit testing sys.exit call with assertRaises or mock.patch

I've read a lot of answers suggesting that the way to unit test sys.exit is to do the following: with self.assertRaises(SystemExit) as system_exit: function() self.assertEqual(system_exit.exception_code, 1) Does the above not actually raise a…
cubestack
  • 11
  • 1
1
vote
1 answer

Using a config/json file to generate tests in Python

I'm working on building a test suite in Python using unittest for a small project and I'm building a bunch of tests to use different switches. The code for the tests is very reusable, and it would be a waste to keep copying the test over and…
vp199090
  • 33
  • 4
1
vote
1 answer

Mock a function passed as default parameter

I have a python file (a.py) which defines a function, and a class which uses it as a default parameter in it's init method and initializes another imported class. This is my a.py import OtherClass def useful_default_func(): //do something…
saurbh
  • 431
  • 5
  • 13
1
vote
2 answers

How to use mock_open with pickle.load

I want to implement unit test for a function : def load_pickle(path): with open(path, 'r') as of: return pickle.load(of) I adapted what I found on https://nickolaskraus.org/articles/how-to-mock-the-built-in-function-open/ to implement…
luc.soret
  • 21
  • 4
1
vote
0 answers

Python Unittest: Tests with parameters?

I'm working on creating tests for the first time using the unittest package in Python. The part of the project I'm writing tests for now is a creation of a new database file (SQLite) and ensuring the various tables were set up properly. I have…
Jason Harrer
  • 107
  • 5
1
vote
2 answers

Mock a class before importing it

I'm trying to mock a method in a parent class that is declared out of any class method. The problem es I can't figure out how to make the LoggerUtils class to be instantiated in the parent class. Puting it inside an __init__ is not an option due to…
1
vote
0 answers

PyTest marker for subset of Python-Unittest ddt test cases

I have test cases written in Python-Unittest and executing them with PyTest. I want to add PyTest marker to subset of ddt test cases. But currently able to use PyTest marker for whole test case. Like this - # cat unittest_marker.py import unittest,…
va1bhav
  • 365
  • 1
  • 5
  • 21
1
vote
1 answer

How is it possible to pass two positional arguments to unittest.patch when it only accepts one?

I'm writing tests for a Home Assistant integration using tests from another integration as an example. There is a method that is called from each test function that is some how calling patch with two positional arguments: async def…
shred
  • 13
  • 3
1
vote
1 answer

When to use BDD and when just unittests?

I have a task to write tests for future Django Channels+DRF project, don't ask why (we only have swagger documentation for now). So the tests have to test the user use cases (like scenario that may be complex). I have researched about that and found…
1
vote
1 answer

Execution order of python unitests by their declaration

I'm using python unittests and selenium and in my code I have one test class with many testcases: class BasicRegression(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver =…
lukos06
  • 187
  • 1
  • 4
  • 20
1
vote
0 answers

How to patch a module that hasn't been imported by parent package's __init__.py

I'm trying to test a tool I'm building which uses some jMetalPy functionality. I had/have a previous version working but I am now trying to refactor out some external dependencies (such as the aforementioned jMetalPy). Project Code & Structure Here…
Folarin
  • 11
  • 1
  • 2
1
vote
2 answers

How do I mock out file writing to multiple files in python

I'm trying to test a function in which one call results in multiple files being written: def pull_files(output_files=[]): for output_file in output_files: content = get_content_from_server(output_file) with open('/output/' +…
Andy
  • 3,228
  • 8
  • 40
  • 65
1
vote
0 answers

AssertionError: "'str' object has no attribute 'get'"

I'm new in Python. I have a class DatabaseConfig contains some information of database config. I writed a test case by using Unittest for it with the script is if dbopt is invalid (say 'dbopt' : 'abc') then expected output is Throw exception:…
BinhDuong
  • 79
  • 2
  • 15
1
vote
1 answer

Can I "turn-off" @unittest.expectedFailure?

Let's say that, for whatever reason, I want to write a set of test cases that start out from the premise of failing code. Maybe because setting up a failure is really complicated, while the demonstrating a fixed state is simple. In my case, it's…
JL Peyret
  • 10,917
  • 2
  • 54
  • 73
1
vote
0 answers

Python assert_has_calls fails with order of attributes of object parameter different

I have an assertion like this - obj_instance_1 = SomeType(a=1,b=2,c=3) //invoke the test function mock_calls = [ mock.call(param1=obj_instance_1, param2=mocked_obj), mock.call(param1=obj_instance_1, param2=mocked_obj), …
krackoder
  • 2,841
  • 7
  • 42
  • 51
1 2 3
99
100