Public Group

# 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.

• ### Similar Content

• I'M interested in programming tools (For animation, UI, etc). Can anyone suggest me the resources where I can start learning or which technologies I need achive it.

Thanks,
Rakshit
• By Mogli
Hello!
As my hobby, I’m working on an ice-hockey game, using Python (pygame). It will be based on wego-turns, with each turn representing 3 seconds of “realtime playback”. In other words: both users plan their skaters’ movements and actions for the upcoming 3 seconds while the game is paused, and then, when they both have confirmed their turn, the game takes their plans, calculates what happens and finally presents the outcome of the turn to the users as a 3 seconds-replay. All frames of the action are stored so that users can re-watch the action, using rewind, forward, play functions etc.
As I'm almost totally new to programming, I’m worried about the planning phase. Although my code does achieve what I want, I’m worried that it is overly complicated and will give me lots of headaches later on. Therefore, I would be very thankful if someone more experienced in programming could take a look and/or lend me a hand and give me tips how to simplify and shorten it. I will post my code at the end of the post in the spoiler, but I fear that it might be so messed up that noone but me understands it.
Here is what the code should do:
IDEA
Context: When a certain input is given while a skater is selected during the planning phase, planning mode is enabled.In planning mode, the user can set waypoints for the selected skater. The higher the speed of a skater, the narrower the allowed angle to set the next waypoint needs to be. E.g. if your skater goes at full speed, you cannot perform a 90° turn within just two waypoints. The number of waypoints that can be set for a skater in the planning phase depends on the skater’s speed. The greater the speed, the more waypoints can be set. The effect of speed needs to be tracked during the planning-phase itself, so that - for example - if a user sets acceleration-waypoints he will be able to set more waypoints in this very planning-phase. All information for movement (between waypoints) must be stored, as it needs to be retrievable in the replay-phase. If you have any ideas or hints how to come up with a code that achieves these things, please tell me, never mind how basic or in-depth! What follows below is my complicated attempt at it.
MY ATTEMPT
VIDEO

• Where to get started in making a text based MUD?

So I have been making small games for over 3 years now and I'm comfortable in the programming languages that follow:
1. Python (I need a bit of practice with this)
2. C# (I am the most comfortable in this)
3. Java (I know quite a bit but might need to do a tad of research)

and I have also done a bit in:
4. JS (I know pretty much nothing here)
5. And some other stuff

Anyway, so now you know all that rubbish here goes. What I wan't to do is program a text based MMORPG or a text based MUD. I have basically no clue where to begin so I what I need is some places to start. If anyone has any information on the subject, anything at all, please post it bellow. I will be really appreciated.

• A simple arcade game in python.I am currently in class 9th and made this game using python

• I've made a very simple map generator as a demo for combat encounters in my game. I plan to have the trees and rocks be cover, while the player's bandits ambush a caravan on the road in the middle. Please, let me know what you think. Criticism is welcome.

• Hello all,
I have teamed up with TheGameCreators for them to trial a Python version of AppGameKit in the form of a PYD file that makes AppGameKit commands available to Python 3.6 and greater.  Currently this PYD is compiled only for use with the Windows x86 version of Python.  It is a free, unlimited version of their commercial product, but it does show the AppGameKit logo for a few seconds on game startup and AppGameKit is mentioned in the window title when in windowed mode.  The PYD works with PyInstaller for easy distribution.
If you haven't heard of AppGameKit, I would recommend looking over their website.  They also have documentation online and an active community forum.  I should point out that method names have been changed to follow PEP 8 naming conventions, so they will be similar but different from the online help.  I also had to make a few changes mentioned on the project's itch.io page.  There is also a PYI file there that can be used with PyCharm for easier access to the same information.
As mentioned, this is a trial for them, but if there's a positive response they will extend the project and include a way of compiling an executable without the intro logo and window title text as well as support other OS.
Please take a look and feel free to ask me any questions about it here or on the project's community forum on itch.io.
AppGameKit for Python project page at itch.io: https://fascimania.itch.io/appgamekit-for-python
Original announcement url: https://www.thegamecreators.com/post/announcing-appgamekit-for-python

• I want to make a random map generator in python 3, but I have no idea how to do it. Could someone recommend me a good starting point?
What I want to make is a flat 2D overworld-map with roads, rivers, settlements, farmlands, forests, and plains.
I don't want to model specific buildings. This is just for a game I'm making where I'd like the player to be able to travel from point to point.

• 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!
• By Josheir
I was looking for a library to make a server/clients program.  I was thinking there would be easy tutorials for Javascript or Java.  What I ran across that is interesting is a Python tutorial for teens that looks pretty easy.     https://www.raywenderlich.com/38732/multiplayer-game-programming-for-teens-with-python
I don't know too much about Python though and I am wondering if the language is worthwhile.  Is it supported?  Also, I am interested in the possible use of a relational database, of which I found :  PostgreSQL.
If you know Python could you please give me a recommendation.  Its future, use, popularity, IDE, Database suggested?  From what I see it looks like an understandable language.
One of the highly recommended IDEs called Pycharm has no database support, so I stopped here and thought I'd bring it up in this forum.

I'm interested in any free versions too,
Thank you; maybe you have alternate suggestions?
Josheir

• 16
• 11
• 10
• 9
• 49
• ### Forum Statistics

• Total Topics
631392
• Total Posts
2999743
×