unit-testing

How to mock stdin when using fileinput module?

How to mock stdin when using fileinput module? Question: I have a program that uses the Python fileinput module, and I am trying to write unittests for the main() function. They work find when using an actual file, but raise OSError: reading from stdin while output is captured when I try to pass data via …

Total answers: 2

assertEqual vs assertSetEqulal in unittest

assertEqual vs assertSetEqulal in unittest Question: Is there a difference between assertEquals and assertSetEqual in the python unittest.TestCase for assertion of sets or frozensets? And if there is not, why are there assertSetEqual? also for this situation we can use assertCountEqual and assertSequenceEqual! . . . self.assertEqual({1, 2, 3}, {1, 2, 3}) self.assertSetEqual({1, 2, 3}, {1, 2, 3}) . . …

Total answers: 1

How do i mock an external libraries' classes/functions such as yaml.load() or Box() in python

How do i mock an external libraries' classes/functions such as yaml.load() or Box() in python Question: How would i go about testing the following class and its functions? import yaml from box import Box from yaml import SafeLoader class Config: def set_config_path(self): self.path = r"./config/datasets.yaml" return self.path def create_config(self): with open(r"./config/datasets.yaml") as f: self.config = …

Total answers: 1

AssertionError – Creating my own python library and I am getting an assertion error

AssertionError – Creating my own python library and I am getting an assertion error Question: I am coding an encryption library that allows you generate either symmetric or asymmetric key/key pair and use it within Either of these 4 classes: TextEncA, TextEncS, ImageEncA and ImageEncS. The code looks to be syntactically correct however while testing …

Total answers: 1

Python unit tests – mocking imported class methods

Python unit tests – mocking imported class methods Question: I would like to mock some imported class methods and module functions for my unit tests. I tried several ways to define the mocked values but I don’t understand why they are not taken into account. I wrote some tests following the advices in Python Mocking …

Total answers: 1

Unexpected millisecond offsets when doing subtraction with datetime and timedelta objects

Unexpected millisecond offsets when doing subtraction with datetime and timedelta objects Question: I have this class implemented in python on which i am performing some unitests from datetime import datetime, timedelta class FreeTime: """Range in a timeframe where a task can be located""" def __init__(self, start: datetime, end: datetime) -> None: self.start = start """ …

Total answers: 2

Python unittest case expected not matching with the actual

Python unittest case expected not matching with the actual Question: I am trying to mock the secrets manager client. Earlier the variables weren’t in the class so I was able to mock the client directly using a patch like below: @patch(‘my_repo.rc.client’) and now since I am using an instance method, I need to mock the …

Total answers: 1

Python Unit test a function imported by the function under test

Python Unit test a function imported by the function under test Question: I’m attempting to implement thorough testing of code that calls to a database at various at points. I’m able to mock the calls that occur in the function under test, but I’m now also needing to stub out functions that are imported by …

Total answers: 1

Mock object with pyTest

Mock object with pyTest Question: I have a function I need to test: def get_user_by_username(db, username: str) -> Waiter: """Get user object based on given username.""" user = db.query(Waiter).filter(Waiter.username == username).first() return user Here I try to mock DB call and return correct Waiter object, so my test is: def test_get_user_by_username(mocker): waiter = Waiter(id=10, username="string", …

Total answers: 1