I have been playing around with the interface and testing recently. I'm not doing any checks for layout or aesthetics, since those can be done by humans much more better than by computer (at least if I'm the one coding). But I figured out neat way to check if the correct information is shown to player and if basic functionality of a dialog is working.
Following snippet creates a character, who has cure minor wounds spell cast on him (that heals player slowly over couple of turns). It then creates a widget (control) that can show player's stats, spells being among them. Last two lines checks that there are two strings present at the dialog: name of the spell and short description.
It doesn't matter where those two strings are present, as long as they are there. This is just a quick check that the dialog can show the information to player. Human can then do the more complex testing if required.
def test_displaying_effects(self):
"""
Displaying character should show effects
"""
character = (CharacterBuilder()
.with_body(5)
.with_finesse(6)
.with_mind(7)
.with_effect(EffectBuilder()
.with_title('Cure minor wounds')
.with_description('Cures small amount of damage')
)
.build())
surface_manager = mock()
when(surface_manager).get_icon(any()).thenReturn(QPixmap())
widget = CharacterWidget(surface_manager = surface_manager,
character = character,
parent = None)
assert_that(widget, has_label('Cure minor wounds'))
assert_that(widget, has_label('Cures small amount of damage'))
Second test has a character standing in the same location with a dagger. Test creates inventory widget that shows the player's equipment, inventory and items laying on the ground. It then locates control which has dagger in it and clicks it. Final step is to check that the dagger is no longer laying on the ground.
What I like about the second example is how I don't have to tell exactly where to click, the test can figure it out by itself (within limits, of course). If I were to move things around on the screen, it should still work, as long as the basic idea stays the same.
def setup(self):
"""
Setup test case
"""
self.application = QApplication([])
self.surface_manager = mock(SurfaceManager)
when(self.surface_manager).get_icon(any()).thenReturn(QPixmap())
self.action_factory = (ActionFactoryBuilder()
.with_inventory_factory()
.build())
self.level = (LevelBuilder()
.build())
self.character = (CharacterBuilder()
.with_level(self.level)
.with_location((5, 5))
.build())
def test_picking_up_item(self):
"""
Test that item can be picked up
"""
item = (ItemBuilder()
.with_name('dagger')
.build())
self.level.add_item(item, (5, 5))
dialog = InventoryDialog(surface_manager = self.surface_manager,
character = self.character,
action_factory = self.action_factory,
parent = None,
flags = Qt.Dialog)
QTest.mouseClick(satin.widget(dialog,
slot_with_item('dagger')),
Qt.LeftButton)
assert_that(self.level, does_not_have_item(item.name))