diff options
author | Daniel Latypov <dlatypov@google.com> | 2020-12-02 11:08:21 -0800 |
---|---|---|
committer | Shuah Khan <skhan@linuxfoundation.org> | 2021-02-08 15:33:32 -0700 |
commit | cfd607e43da4a20753744f134e201310262b827a (patch) | |
tree | 6d54478b251218123d18a0cae109e90a6b40f314 /tools/testing/kunit | |
parent | 92bf22614b21a2706f4993b278017e437f7785b3 (diff) | |
download | linux-cfd607e43da4a20753744f134e201310262b827a.tar.gz linux-cfd607e43da4a20753744f134e201310262b827a.tar.bz2 linux-cfd607e43da4a20753744f134e201310262b827a.zip |
kunit: tool: fix unit test cleanup handling
* Stop leaking file objects.
* Use self.addCleanup() to ensure we call cleanup functions even if
setUp() fails.
* use mock.patch.stopall instead of more error-prone manual approach
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Tested-by: Brendan Higgins <brendanhiggins@google.com>
Acked-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Diffstat (limited to 'tools/testing/kunit')
-rwxr-xr-x | tools/testing/kunit/kunit_tool_test.py | 14 |
1 files changed, 6 insertions, 8 deletions
diff --git a/tools/testing/kunit/kunit_tool_test.py b/tools/testing/kunit/kunit_tool_test.py index b593f4448e83..9a036e9d4455 100755 --- a/tools/testing/kunit/kunit_tool_test.py +++ b/tools/testing/kunit/kunit_tool_test.py @@ -288,19 +288,17 @@ class StrContains(str): class KUnitMainTest(unittest.TestCase): def setUp(self): path = get_absolute_path('test_data/test_is_test_passed-all_passed.log') - file = open(path) - all_passed_log = file.readlines() - self.print_patch = mock.patch('builtins.print') - self.print_mock = self.print_patch.start() + with open(path) as file: + all_passed_log = file.readlines() + + self.print_mock = mock.patch('builtins.print').start() + self.addCleanup(mock.patch.stopall) + self.linux_source_mock = mock.Mock() self.linux_source_mock.build_reconfig = mock.Mock(return_value=True) self.linux_source_mock.build_um_kernel = mock.Mock(return_value=True) self.linux_source_mock.run_kernel = mock.Mock(return_value=all_passed_log) - def tearDown(self): - self.print_patch.stop() - pass - def test_config_passes_args_pass(self): kunit.main(['config', '--build_dir=.kunit'], self.linux_source_mock) assert self.linux_source_mock.build_reconfig.call_count == 1 |