In-Game Tests#

krpctools runs tests against a live copy of KSP. krpc-game installs kRPC and its test add-on into a KSP install and runs the game. In a pytest suite, the game runs with the mods each test class requires. Both run on Linux and Windows.

The Game#

Set KSP_DIR to a KSP install, or pass --game-dir, then run:

krpc-game run --load-game=krpctest --mods=FAR

This installs the kRPC release your krpctools was built with, the test add-on and the FAR mod. It then launches KSP, loads the krpctest save, created blank on the first run, and prints the game’s kRPC log lines. krpc-game stop, from another terminal, stops the game. krpc-game install installs without launching.

To test against another kRPC release, install the krpctools of the same version. --krpc installs a kRPC archive such as a local build. The bundled test add-on is built against the release, so it may fail to load with another build.

--mods names the supported mods to install, and each one brings the mods it needs. The install removes the other supported mods. Run krpc-game run --help for the options that load a save, switch vessel or launch a craft.

Note

The tests need GameData to hold only the stock game, kRPC, ModuleManager and the requested mods. An install with anything else fails, unless you pass --skip-install-check.

Test Suites#

A test suite chooses its game in conftest.py. This one tests the fictional Heatshields mod, built into build/Heatshields.zip beside it:

import os

from krpctools.game import register
from krpctools.game.ksp import KSP, Mod


class Heatshields(Mod):
    name = "Heatshields"
    folders = ("Heatshields",)

    def present(self, game, conn):
        return conn.testing_tools.part_available("heatshield.large")


build = os.path.join(os.path.dirname(__file__), "build")
register(KSP(mods=[Heatshields(os.path.join(build, "Heatshields.zip"))]))

A mod names the folders it installs into GameData, and in files any files it puts at the top of GameData. generated names the files it writes at the top of GameData while the game runs. present returns whether the running game has the mod. By default it checks that the game database holds files from each of the mod’s folders, and returns None for a mod with no folders. A mod that needs another mod names it in requires.

Each test class names the mods it requires. Before its setUpClass runs, KSP is running with exactly those mods and the mods they require:

from krpctools.game.ksp import TestCase


class TestHeatshields(TestCase):
    mods = ["Heatshields", "FAR"]

    @classmethod
    def setUpClass(cls):
        cls.vessel = cls.launch("Capsule")

    def test_heatshield(self):
        parts = self.vessel.parts.with_name("heatshield.large")
        self.assertEqual(1, len(parts))

The class fails when the integration of a required mod, matched by name, failed to load or is missing members of the installed version.

launch loads a new save, launches the craft, removes the other vessels and returns the active vessel. It takes the craft from a craft directory beside the test file, or from the game’s stock craft. directory names another directory, and editor="SPH" launches onto the runway. The craft takes its default crew, or the Kerbals named in crew, and its crew become pilots. save=None launches in the current save, and keep_other_vessels=True keeps the vessels already there. The TestCase also places a vessel in orbit, on the ground or in flight, and has assertions with a tolerance. wait_game_time waits for game time to pass, which takes longer than real time in a slow game. krpctools.game.images checks the PNG and JPEG images an RPC returns.

Run the suite with the plugin loaded:

pytest -p krpctools.game.pytest_plugin

The plugin orders the tests by the mods they require, so the game restarts at most once per mod set. The run restarts, and installs into, only a game it launched.

Options#

krpc-game and the pytest plugin take these options:

Option

Effect

--game

The game: ksp, or module:object for a game object in Python code. krpc-game defaults to ksp, and the plugin to the game conftest.py registers.

--game-dir

The game’s install directory, in place of KSP_DIR.

--krpc

The kRPC archive to install, in place of the release krpctools was built with.

--mod NAME SOURCE

Installs a mod from a path or a URL, in place of its latest pinned archive.

--skip-install-check

Allows mods outside the requested set in GameData.

--skip-mod-install

Leaves the installed mods as they are.

--no-launch

Runs the tests against a game already running. Plugin only.

--address

The address of the game’s server. Plugin only.

Downloads are cached in the user cache directory, such as ~/.cache/krpctools, so a repeat install needs no network.