Writing unit tests for pygame

Over the last few weeks I have been getting an urge to get more involved with open source projects that aren't my own. So after finding this post on Reddit, went over to pygame's repository straight away and looked for an issue I could start working on. Since a lot of the code is in C I decided it would be best to start off writing unit tests in Python and get familiar with the project first, so that's what I did!

Ellipses

As stated in issue #233 pygame had (it has now been fixed by someone else) a problem where ellipses with an odd width and height were being drawn 1 pixel smaller than they should. As you can see in the image below, an ellipse with an even height and width is drawn correctly




A 4x4 ellipse on a 4x4 surface

However, when drawing an ellipse with and odd height or odd width is not drawn correctly.




A 5x5 ellipse on a 5x5 surface

Solution

The unit test I created for this was one that creates a surface of a certain width and height, then creates an ellipse with the same width and height on the surface. Since the surface and the ellipse are the same size, you would expect all four sides of the ellipse to be touching the edge of the surface, right? So it then creates four lists containing the values of the top, left, right and bottom borders respectively. Finally it checks if each of these lists contains the colour of the ellipse, and if they don't then the test fails of course.

Issue(s):
https://github.com/pygame/pygame/issues/233

Pull request(s):
https://github.com/pygame/pygame/pull/501

More ellipses and lines

After discussing the unit test I had written on the pygame Discord with someone (the guy who would eventually fix the problem) I came to the conclusion that I should write a unit test that is more comprehensive.

Ellipse

I updated the draw.ellipse unit test so that it now also includes a test that checks if at least two sides of an ellipse, that has a width and height that is 1 pixel smaller than the surface, is touching the surface's border.

It also cleans up the code slightly, including a better way of retrieving the border's values and adds reusability in the form of additional functions.

(Aa)line(s)

While updating the aforementioned unit test I was also looking at issue #395, which described that lines drawn with the pygame.draw.aalines() function seem to be washed out of the blue color.




A washed out blue aaline

The way I tackled this unit test was by creating all possible surfaces (ARGB, RGBA) with and without SRC_ALPHA, with different sizes and depths, then drawing aaline(s) on it of a certain colour, then checking if the colour was drawn correctly. To make sure this wasn't just a problem for aaline(s), I also added tests for the draw.line functions as well.

So in summary - for draw.aaline and draw.line it now draws a line of differing colours on all types of surfaces and checks if the colour stays the same at position (0, 0).

The same is done for draw.aalines and draw.lines, except these tests draw a line around the border of the given surface then check if all pixels are of the given colour.

Gaps?!




Aaline(s) that contain gaps

While doing some research about this I was running my code and noticed that some of the aaline(s) either weren't being drawn at all or contained gaps, so I decided to also create an issue for this (#512) and write unit tests for it as well. So this means there is also a test that checks if the line contains any gaps (or is not drawn at all when close to a surface's border). Once again the same is done for draw.aalines and draw.lines.

Issue(s):
https://github.com/pygame/pygame/issues/233
https://github.com/pygame/pygame/issues/395
https://github.com/pygame/pygame/issues/512

Pull request(s):
https://github.com/pygame/pygame/pull/510

My experience

All in all it was very fun discussing potential solutions on pygame's Discord with other contributors. I must say that I also learnt a lot doing this, and also having my code reviewed by others has helped me improve, so I would definitely recommend that you start contributing to other people's (open source) projects as well!

Repository

https://github.com/pygame/pygame/

GitHub Account

https://github.com/amosbastian

H2
H3
H4
Upload from PC
Video gallery
3 columns
2 columns
1 column
9 Comments