Writing unit tests for pygame

in pygame •  6 months ago

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!


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


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.


Pull request(s):

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.


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.


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.


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.


Pull request(s):

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!



GitHub Account


Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congrats - having a couple of commits on well-known projects like Pygame always feels good. :)

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.

Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.


Thank you for your review, @emrebeyler!

So far this week you've reviewed 3 contributions. Keep up the good work!

Pygame is an awesome library. It's super useful as a teaching tool as well.


I hadn't heard of if before I found the Reddit post, but it definitely looks really cool. Just have to wrap my head around some of the C code (I really neglected learning it properly at university) and then I'll have an attempt at actually trying to fix some of the issues as well as writing unit tests!


Yea, I have never played around with the C code, I have only used the library when using python.

Grats! Awesome post :)

Congratulations! Your post has been selected as a daily Steemit truffle! It is listed on rank 15 of all contributions awarded today. You can find the TOP DAILY TRUFFLE PICKS HERE.

I upvoted your contribution because to my mind your post is at least 5 SBD worth and should receive 95 votes. It's now up to the lovely Steemit community to make this come true.

I am TrufflePig, an Artificial Intelligence Bot that helps minnows and content curators using Machine Learning. If you are curious how I select content, you can find an explanation here!

Have a nice day and sincerely yours,

Hi @amosbastian!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @amosbastian!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!