Category : python-unittest

Given a folder structure like such: dags/ **/ code.py tests/ dags/ **/ test_code.py conftest.py Where dags serves as the root of the src files, with ‘dags/a/b/c.py’ imported as ‘a.b.c’. I want to test the following function in code.py: from dag_common.connections import get_conn from utils.database import dbtypes def select_records( conn_id: str, sql: str, bindings, ): conn: ..

Read more

After update pytest to pytest-remotedata-0.3.2, my tests stop working correctly. Still have the same issue: start_time = datetime.fromtimestamp(test_method._outcome.result.start_time) AttributeError: ‘TestCaseFunction’ object has no attribute ‘start_time’ code: def finish_test(self, test_method, driver=None): test_status = "OK" try: start_time = list(test_method._outcome.result.test_started_datetime_map.values())[-1] except AttributeError: start_time = datetime.fromtimestamp(test_method._outcome.result.start_time) Any ideas how i can replace start_time = datetime.fromtimestamp(test_method._outcome.result.start_time) Source: Python-3x..

Read more

Problem I cannot get VSCode/unittest to recognize submodules (i.e., common.nested_module in the example in background) that I wrote unittests for. VSCode discovers the tests cases and executes those in test_my_module.py but complains that there is no module common.nested_module on the import statement line in test_nested_module.py (in Output: ModuleNotFoundError: No module named ‘common.nested_module’). I added print(sys.path) ..

Read more

I’m trying to test an api mock call using from unittest.mock import patch. I keep getting an AttributError when I include .json() in my function’s response return (i.e. return response.json()). When I exclude the .json() in my return statement, the test passes. I’m pretty new to testing and I can’t figure out how to get ..

Read more

Take this example, func_to_test_script.py from module import ClassA def func_to_test(): instance_b = ClassA.method_a() foo = instance_b.method_b() method_a returns an instance of class ClassB. I want to mock the calls of methods method_a and method_b, so I do @mock.patch("func_to_test_script.ClassA", ClassAMock) @mock.patch("func_to_test_script.ClassB", ClassBMock) def test_func_to_test(self): # test I can do this with ClassA (that was imported in ..

Read more

I have a dummy funtion to try exceptions: def fun(n): try: if n <1: raise ValueError return 1 except: pass In my unit test I use: import unittest class TestFibonnacci(unittest.TestCase): def test_values(self): self.assertEqual(fun(1),1) self.assertRaises(ValueError, fun(-1)) However I’m unable to get an answer I actually get: An exception occurred E…. ERROR: test_values (main.TestFibonnacci) Traceback (most recent ..

Read more

Can someone help me to unit test this line of code? from datetime import datetime, timedelta, timezone def get_timestamp_plus_100_year(): return int((datetime.now(timezone.utc) + timedelta(days=100 * 365)).timestamp()) I try this, but i dont know how to assigns the values: @patch("src.shared.utils.timedelta") @patch("src.shared.utils.datetime") def test_get_timestamp_now_plus_100_years(self, mock_datetime, mock_timedelta): mock_datetime.now.return_value = 2021-09-14 15:54:25.284087+00:00 mock_timedelta.return_value = 36500 days, 0:00:00 self.assertEqual( get_timestamp_plus_100_year(), int( ..

Read more