Skip to content
Chris Oelmueller edited this page Feb 15, 2013 · 1 revision

This page gives a quick overview of how to write gui tests in Unknown Horizons

1. Start the game with GUI logger enabled

python run_uh.py --sp-seed=42 --gui-log --start-dev-map

It is important (or at least a good idea) to use the same RNG seed as the tests '''--sp-seed=42'''

Enable the logger '''--gui-log'''

Use '''--start-dev-map''' or '''--load-map''' to jump directly into the game, this way you can avoid to log menu interactions.

2. Use the output

When interacting with the game, code will be printed on stdout

Example:

gui.cursor_click(64, -6, 'left')                         # selected the ship
gui.cursor_click(60, 0, 'right')                         # sent ship to (60, 0)
gui.trigger('overview_trade_ship', 'found_settlement')        # select the 'build settlement' button

gui.cursor_click(59, 4, 'left')                          # place a warehouse at (59, 4)

3. Use generated code in a test case

Testcode template:

@gui_test(use_dev_map=True)
def test_example(gui):
    # insert your code here

Example:

@gui_test(use_dev_map=True)
def test_example(gui):
    # Units cannot be selected right now, you need to do it this way. This is almost
    # the same as selecting it with the mouse
    ship = get_player_ship(gui.session)
    gui.select([ship])

    gui.cursor_click(60, 0, 'right')                         # sent ship to (60, 0)
    while (ship.position.x, ship.position.y) != (60, 0):     # wait for the ship to arrive at its destination
        gui.run()

    gui.trigger('overview_trade_ship', 'found_settlement')        # click the 'build settlement' button
    gui.cursor_click(59, 4, 'left')                          # place a warehouse at (59, 4)

Examples

Documented example: https://github.com/unknown-horizons/unknown-horizons/blob/master/tests/gui/test_example.py

Real tests: https://github.com/unknown-horizons/unknown-horizons/tree/master/tests/gui/ingame

Notes

  • Selecting units with the mouse does not work, obtain a reference to the instance and use gui.select([instance])
  • Code for handling dialogs is generated, but the generator will fail in some cases, e.g. credits window
  • once the test function returns, the game will quit. if you want to see the results of the test, insert a gui.run(2**10) for example
Clone this wiki locally