Note that there are some explanatory texts on larger screens.

plurals
  1. POhow should I test method that does not return anything?
    text
    copied!<p>This is the code from my recent tool I have made, I am trying to write unittest, I have an <a href="https://github.com/sanfx/slideShow/blob/master/tests/test_utils.py" rel="nofollow">idea of how to test a method that returns something</a> but don't understand how should i test method that don't like below:</p> <pre><code>def buildUi(self): self.label = QtGui.QLabel() self.label.setAlignment(QtCore.Qt.AlignCenter) self.setCentralWidget(self.label) def nextImage(self): """ switch to next image or previous image """ if self._imagesInList: if self._count == len(self._imagesInList): self._count = 0 self.showImageByPath( self._imagesInList[self._count]) if self.animFlag: self._count += 1 else: self._count -= 1 def showImageByPath(self, path): if path: image = QtGui.QImage(path) pp = QtGui.QPixmap.fromImage(image) self.label.setPixmap(pp.scaled( self.label.size(), QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)) def playPause(self): if not self._pause: self._pause = True self.updateTimer.start(2500) return self._pause else: self._pause = False self.updateTimer.stop() def keyPressEvent(self, keyevent): """ Capture key to exit, next image, previous image, on Escape , Key Right and key left respectively. """ event = keyevent.key() if event == QtCore.Qt.Key_Escape: self.close() if event == QtCore.Qt.Key_Left: self.animFlag = False self.nextImage() if event == QtCore.Qt.Key_Right: self.animFlag = True self.nextImage() if event == 32: self._pause = self.playPause() </code></pre> <p>the complete code for looking can be found <a href="https://github.com/sanfx/slideShow/blob/master/python/slideShow.py" rel="nofollow">here</a></p> <p>is it possible to test these methods above or do I have to modify to make them testable ? Edit: updated:</p> <pre><code>class TestSlideShow(unittest.TestCase): """ docstring for TestSlideShow """ def setUp(self): self.mox = mox.Mox() self.imgLst = ['/folder/test/images/test1.jpg', '/folder/test/images/test2.JPG', '/folder/test/images/test3.png', '/folder/test/images/test4.PNG'] app = QtGui.QApplication([]) self.show = slideShow.SlideShowPics(imgLst=self.imgLst, ui=False) def tearDown(self): self.mox.UnsetStubs() self.mox.ResetAll() def test_nextImage(self): self.mox.StubOutWithMock(self.show, 'prepairWindow') self.show.prepairWindow() self.mox.StubOutWithMock(self.show, 'showImageByPath') self.show.showImageByPath(self.imgLst[1]) self.show.nextImage() # self.mox.ReplayAll() self.assertEquals(1, self.show.count) self.assertEquals(self.imgLst[1], self.show._imagesInList[1]) # self.mox.VerifyAll() def test_nextImage_animFlag_False(self): self.show.animFlag = False self.show.count = 4 self.mox.StubOutWithMock(self.show, 'prepairWindow') self.show.prepairWindow() self.mox.StubOutWithMock(self.show, 'showImageByPath') self.show.showImageByPath(self.imgLst[3]) print self.show.count self.show.nextImage() print self.show.count # self.assertEquals(3, self.show.count) self.assertEquals(self.imgLst[3], self.show._imagesInList[3]) if __name__ == '__main__': unittest.main() </code></pre> <p>the first test when self.show.animFlag is True works fine and but the when I manually set the animFlag= False then second test fails.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload