Skip to content

Coming up with guidelines on how we write tests (3410) #1681

Open
@GalacticEmperor1

Description

@GalacticEmperor1

Issue №3410 opened by illume at 2022-08-20 08:27:52

Was thinking about improvements to our tests to make them easier to read.

Many of the tests were written in a time before best practices were known, and also before python even included the unit test module! We never really made a guide of how we should do them. Some files follow different styles a bit too.

Here's one example from the math tests (which follows the style of the other tests in that file):

    def test_move_towards_max_distance(self):

        expected = Vector3(12.30, 2021, 42.5)

        origin = Vector3(7.22, 2004.0, 17.5)

        change_ip = origin.copy()



        change = origin.move_towards(expected, 100)

        change_ip.move_towards_ip(expected, 100)



        self.assertEqual(change, expected)

        self.assertEqual(change_ip, expected)

A few ideas:

def test_move_towards_does_not_go_past(self):

    """It should stop at the destination vector and not go past it."""



    origin = Vector3(7.22, 2004.0, 17.5)

    destination = Vector3(12.30, 2021, 42.5)

    distance_too_far = 100

    self.assertEqual(origin.move_towards(destination, distance_too_far), destination)

    self.assertNotEqual(origin.distance_to(destination), distance_too_far, "we didn't go that far")



def test_move_towards_does_not_go_past_ip(self):

    """It should stop at the destination vector and not go past it. In place operation."""



    origin = Vector3(7.22, 2004.0, 17.5)

    destination = Vector3(12.30, 2021, 42.5)

    too_far = 100

    origin.move_towards_ip(destination=destination, distance=too_far)

    self.assertEqual(origin, destination, "it changed in place")

    self.assertNotEqual(origin.distance_to(destination), too_far, "we didn't go that far")
  • how the test should be named

  • Naming the variables (distance = 100)

  • testing one thing at a time

  • use keyword arguments

  • using the message part of the assertion (and how that should be written)

  • using the test doc string (and how that should be written)

  • using keyword arguments (here with move_towards the argument names are not documented).

This example and list aren't necessarily correct or good, but a starting point. I guess there are a good set of best practices we could settle on. Then those guidelines can be documented, and the big process of going through all the tests for improvement can begin.


Comments

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions