def error_and_exit(title, main_text): """ Show a pop-up window and sys.exit() out of Python. :param title: the short error description :param main_text: the long error description """ show_system_popup(title, main_text) # Exit the program sys.exit(1)
def test_show_system_popup_win(): # in this test "double mocking techniques" has been applied # there are different mocks that will work depending on the target machine's OS # # In case of *nix machine, "@patch_import(modules=['win32api'], MessageBox=MagicMock())" will work. # In case of win machine, "with patch('win32api.MessageBox'):" will work. # # No matter what kind of Mock was used, the line "win32api.MessageBox.assert_called_once()" should work. # # This approach also applies to the test functions below. import win32api with patch('win32api.MessageBox'): # this patch starts to work only in case win32api exists on the target machine show_system_popup('title', 'text') win32api.MessageBox.assert_called_once_with(0, 'text', 'title')
def test_show_system_popup_darwin(): import subprocess with patch('subprocess.Popen'): show_system_popup('title', 'text') subprocess.Popen.assert_called_once_with(['/usr/bin/osascript', '-e', 'text'])
def test_show_system_popup_linux(): import subprocess with patch('subprocess.Popen'): show_system_popup('title', 'text') subprocess.Popen.assert_called_once_with(['xmessage', '-center', 'text'])
def test_show_system_popup_exception(mocked_print): with patch('subprocess.Popen', new=MagicMock(side_effect=ValueError)): show_system_popup('title', 'text') last_call_args = mocked_print.call_args_list[-1] last_argument = last_call_args.args[0] assert last_argument.startswith('Error while')
def test_show_system_popup_unknown(mocked_print): show_system_popup('title', 'text') mocked_print.assert_called_with('cannot create native pop-up for system Unknown')