Skip to content

Instantly share code, notes, and snippets.

@tpulmano
Last active December 12, 2015 02:28
Show Gist options
  • Save tpulmano/4698820 to your computer and use it in GitHub Desktop.
Save tpulmano/4698820 to your computer and use it in GitHub Desktop.
Mock + PyQt4 + unittest Examples
import mock
# Example of patching QErrorMessage. Check that an error message is shown.
@mock.patch("PyQt4.QtGui.QErrorMessage", autospec=True)
def test_onSessionCreateFailed(self, MockErrorMessage):
self.widget.onSessionCreateFailed(ValueError("This is a test"))
self.assertTrue(MockErrorMessage.return_value.showMessage.called)
# example of mocking a method
dummyServer.getAllActiveJobs = mock.Mock(return_value=TEST_JOBS)
# example 2 of mocking a method
@mock.patch.object(QtGui.QDialog, "show")
def test_show(self, mockedMethod):
self.assertFalse(mockedMethod.called)
self.dialog.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment