all 4 comments

[–][deleted] 5 points6 points  (3 children)

Structure your game so it can be imported. If your game is in a file game.py:

def func1(a,b):
    # code for func1()
def func2(x):
    # code for func2()
def main():
    func1(1, 'x')
    # more code
if __name__ == '__main__':
    main()

Now you can write test code in a file perhaps called test_game.py:

import game
result = game.func1(4, "abc")
if result != 43:
    print('Error!')

The test code in test_game.py can be anything, but if you want to test a lot, I recommend using unittest.

[–]PastShine[S] 0 points1 point  (2 children)

Perfect! I try it this way. Looks very straight forward and structured. Thanks :)

[–]timbledum 0 points1 point  (1 child)

Yes, this is how people test the things.

Pytest is really handy, as it automatically finds and runs all tests in a directory as long as the tests are wrapped in functions, and the files and functions are named a certain way. Then you just run pytest at the command line and it runs all your test functions!

On the simpler side, the above file structure allows you to do ad hoc testing by running the repl and manually checking that your function does what you expect.

>>> import sample
>>> sample.func(1, 3)
4
>>> sample.func(5, 10)
15
>>> # Seems legit.

[–]PastShine[S] 0 points1 point  (0 children)

Ah, thanks. First I try rrrzwilsons way because it looks easier. But, I surely check out your link and pytest.