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…
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…
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…
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…
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…
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…
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,…
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…
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…
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 =…
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…
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/' +…
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:…
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…
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),
…