krpc / krpc2

Remote Procedure Calls for Kerbal Space Program 2
Other
46 stars 3 forks source link

[Feature Request] Testing Scaffolding #29

Open dmadisetti opened 1 year ago

dmadisetti commented 1 year ago

I think unit tests are great. Manually testing these endpoints, but it would be nice to ensure we have reproduciblilty. Not sure how this works with the stripped versions of KSP? I also think there likely has to be a bit more in place to actively do proper testing

djungelorm commented 1 year ago

What we do for krpc1, is write tests for the various endpoints as python scripts, then occasionally (manually) run them with a local copy of the game running. I try and run them when a new version of the game comes out, before a new release of the mod or if some large change is made. (The krpc1 tests have fallen into disrepair recently though and need to be updated...)

The tests use a python package called krpctest and a service DLL called TestingTools that give you functionality for doing things like loading new save games, loading craft files included with the test, and teleporting vessels into defined orbits. This makes setting up the game ready to run the tests more automatic. You just manually start the game, then run the test script, and it sets up the various vessels/orbits/etc ready for the test to run.

The tests for the krpc1 SpaceCenter service are here: https://github.com/krpc/krpc/tree/main/service/SpaceCenter/test

For example, one of the orbit tests looks like this:

class TestOrbit(krpctest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.new_save()
        cls.space_center = cls.connect().space_center

    ...

    def test_vessel_orbiting_kerbin(self):
        self.set_circular_orbit('Kerbin', 100000)
        vessel = self.space_center.active_vessel
        orbit = vessel.orbit
        self.assertEqual('Kerbin', orbit.body.name)
        self.assertAlmostEqual(100000 + 600000, orbit.apoapsis, delta=50)
        self.assertAlmostEqual(100000 + 600000, orbit.periapsis, delta=50)
        self.assertAlmostEqual(100000, orbit.apoapsis_altitude, delta=50)
        self.assertAlmostEqual(100000, orbit.periapsis_altitude, delta=50)
        self.assertAlmostEqual(100000 + 600000,
                               orbit.semi_major_axis, delta=50)
        self.assertAlmostEqual(100000 + 600000,
                               orbit.semi_minor_axis, delta=50)
        self.assertAlmostEqual(700000, orbit.radius, delta=50)
        self.assertAlmostEqual(2246.1, orbit.speed, delta=1)
        self.check_radius_and_speed(vessel, orbit)
        # self.check_time_to_apoapsis_and_periapsis(vessel, orbit)
        self.assertIsNaN(orbit.time_to_soi_change)
        self.assertAlmostEqual(0, orbit.eccentricity, places=1)
        self.assertAlmostEqual(0, orbit.inclination, places=1)
        # self.assertAlmostEqual(0,
        #                        orbit.longitude_of_ascending_node, places=1)
        # self.assertAlmostEqual(0, orbit.argument_of_periapsis, places=1)
        # self.assertAlmostEqual(0, orbit.mean_anomaly_at_epoch, places=1)
        # self.assertAlmostEqual(0, orbit.epoch, places=1)
        # self.check_anomalies(vessel, orbit)
        self.assertIsNone(orbit.next_orbit)

To get this style of testing working for krpc2 we would need the following:

I can create a bunch of issue to get these various parts implemented.


I don't think getting these to run automatically with each PR using github actions is practical:

We could maybe host our own runner with the above requirements, but I'm not sure it's really worth the effort. The testing approach for krpc1 seemed to work well enough for this kind of testing.