# How do I test code that only calls other functions or that only draws to the screen

## Recommended Posts

Currently, I'm working on recreating connect four for a portfolio of simple games. I'm also writing unit tests for all the code I can. Some of my code functions though only call other functions, or only draw to the screen...

#EXAMPLES...

def draw_refresh_button_text(self):
font = pygame.font.Font(None, 24)
text = font.render("Refresh", True, self.black)
self.displaysurf.blit(
text,
(
self.refresh_button_rect.centerx-self.refresh_button_rect.centerx/self.refresh_button_rect.y,
self.refresh_button_rect.centery-self.refresh_button_rect.centery/self.refresh_button_rect.x
)
)

def draw_disks(self):
for column in self.grid:
for disk in column:
pygame.draw.ellipse(self.displaysurf, disk['color'], disk['rect'])

#ONLY CALLS OTHER FUNCTIONS...
def get_input(self):
inputmanager.InputManager.get_events()
inputmanager.InputManager.check_for_quit_event()
inputmanager.InputManager.update_keyboard_key_state()
inputmanager.InputManager.get_keyboard_input()
inputmanager.InputManager.update_mouse_button_state()
inputmanager.InputManager.get_mouse_input()

How would I go about writing tests for code like this?

Should code like this even be tested?

Should all code be tested?

I'm using the Python unittest module to test. The code here uses Pygame.

##### Share on other sites

Personally, I would not test methods like this via unit tests ... because it is not a "contract" you are going to be committing to memory and counting on as you work through other areas of code.

But there is a strategy for testing this kind of thing that is very easy ... and it is good for other cases that do define contracts that matter more.

You would create a "mock"/test object of the input manager, and your mock version would do something like verify that each method was called once during the test, or even that they were called in order ... the methods wouldn't DO anything real in the mock object, they'd just track the calls.

##### Share on other sites

Thanks for reply, I'll look more into mocking.

##### Share on other sites

In Python is very easy to monkey-patch mock functions and objects so you can test without hitting actual system, network, database, or screen draw calls.  Mocking a method of a class by binding a lambda to a side_effect attribute of the MagicMock lets you get away with injecting captive test data into your system from the bottom and then verifying the results returned by your higher-level functions.

Also, thinking about how you're going to mock required dependencies helps you architect your code better.

## Create an account

Register a new account

• 25
• 11
• 17
• 11
• 13
• ### Similar Content

• Hi all, I am starting to develop a tactics game and ran into a problem I had not thought of. I began by drawing a screen with a hex grid, and this is no big deal. I got that working fine. But, I realized it didn't look quite right. This is because in most strategy games, you're not looking straight down. There is a bit of a tilt. Attached is an example of what I mean. The hexagons on bottom are larger than the hexagons on top, and I'm unsure of how to go about adding this effect. Especially when you consider that some maps may be of different sizes.
I'm not sure if this is the right place to post something like this, but it seems as though some sort of linear transformation would be applied? No? I don't even know where to begin in a problem like this.
Thanks.

• By Arthev
Hey folks, new here
So any comments and criticisms on my pong clone would be appreciated!
Written using Python 3 and Pygame.
Github repo: available here.
Thanks!