Merge lp:~mcfletch/simplegc/testsuite into lp:simplegc

Proposed by Mike C. Fletcher
Status: Merged
Merged at revision: 273
Proposed branch: lp:~mcfletch/simplegc/testsuite
Merge into: lp:simplegc
Diff against target: 42 lines (+36/-0)
1 file modified
tests/test_button.py (+36/-0)
To merge this branch: bzr merge lp:~mcfletch/simplegc/testsuite
Reviewer Review Type Date Requested Status
Sam Bull Pending
Review via email: mp+116183@code.launchpad.net

Description of the change

Hi Sam,

One thing that came up in reviewing the GSOC projects so far is that we are way behind on testing and testability. So, here's a quick skeleton of a test suite. You'd install nose and globalsub (pip install --user nose globalsub, then run nosetests -sv in the root of the checkout to run the (two very simple) test cases.

globalsub isn't necessarily something you need to use, it's just what *I* use for most of my test suites. There are lots of similar projects (look for Python mock).

You'll notice in this test suite that there are very few assertions of expected results. Normally you want your code to be testable in such a way that you can assert functionality without needing to "peek into" the code. That is, if I run ".update( time )" where the mouse is over the button, I expect to see e.g. a "MouseIn" event show up, and I want my test case to test for that. Or I want to be able to call the public API to see what the current state is. That style of suite is not "unit testing", it's closer to functional testing, but for now I'd be happy if we could get a reasonable functional test suite covering the bulk of the functionality. A fairly extensive functional test suite will catch most *major* regressions, and will often point out API issues where you might want to refactor the code to allow for easier sub-classing or to break functionality into smaller units.

Enjoy,
Mike

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'tests'
2=== added file 'tests/__init__.py'
3=== added file 'tests/test_button.py'
4--- tests/test_button.py 1970-01-01 00:00:00 +0000
5+++ tests/test_button.py 2012-07-23 02:56:18 +0000
6@@ -0,0 +1,36 @@
7+from unittest import TestCase
8+import sgc # python setup.py install --user
9+from sgc.locals import *
10+
11+import pygame
12+from pygame.locals import *
13+pygame.display.init()
14+pygame.font.init()
15+
16+import globalsub # pip install --user "globalsub>=1.0.2"
17+
18+class TestButton( TestCase ):
19+ def setUp( self ):
20+ self.screen = sgc.surface.Screen((640,480))
21+ self.simple_button = sgc.Button(
22+ label="Clicky", pos=(100, 100)
23+ )
24+ self.simple_button.add(0)
25+ self.posted_events = []
26+ globalsub.subs( pygame.event.post, self.posted_events.append )
27+ def tearDown( self ):
28+ globalsub.restore( pygame.event.post )
29+ globalsub.restore( pygame.mouse.get_pos )
30+ def test_on_click( self ):
31+ self.simple_button.on_click( )
32+ assert self.posted_events, """Should have posted an event by default"""
33+ def test_update( self ):
34+ def fake_get_pos( ):
35+ return (105, 105)
36+ globalsub.subs( pygame.mouse.get_pos, fake_get_pos )
37+ self.simple_button.update( 23.35 )
38+ # we would *want* to be able to say, e.g.
39+ # that we generated an observable event through the public API
40+ # but we don't currently have any observable behaviour
41+ assert self.simple_button._state == 'over', self.simple_button._state
42+

Subscribers

People subscribed via source and target branches