Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unit test roll die
#1
I am beginner at tests. I am trying to do a unit test for a roll die program.
I have tried many options and failed.

Here is the code:
from random import randint
def run():
    try:
        num = int(input('Enter an number to roll: '))
        print('result: ', randint(1, 6))
        while num != 0:
            num = int(input('Enter an number to roll your die, 0 to quit: '))
            print('result: ', randint(1, 6))
        else:
            return 'Game terminated!'
    except ValueError:
        return 'Only integers are allowed'
#run()
The program just freezes when I run the test program.
What am i doing wrong?
Here is the test part.

import pytest
import rolldie
from unittest import mock


def test_die():
    with mock.patch('builtins.input', input_value=range(1, 10)):
        assert rolldie.run() == range(1, 7)
    with mock.patch('builtins.iput', input_value=0):
        assert rolldie.run() == "Game terminated!"
    with mock.patch('builtins.iput', input_value='a'):
        assert rolldie.run() == "Only integers are allowed"

if __name__ == '__main__':
    pytest.main()
Reply


Messages In This Thread
unit test roll die - by saladgg - Nov-06-2018, 04:41 AM
RE: unit test roll die - by woooee - Nov-06-2018, 05:42 AM
RE: unit test roll die - by stullis - Nov-06-2018, 11:43 AM
RE: unit test roll die - by saladgg - Nov-06-2018, 12:44 PM
RE: unit test roll die - by saladgg - Nov-06-2018, 02:50 PM
RE: unit test roll die - by stullis - Nov-06-2018, 11:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unit Testing Set Up and Use RockBlok 2 456 Jan-08-2024, 07:43 PM
Last Post: deanhystad
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,186 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  Dice Roll (Find out how many rolls until specified streak) DustinKlent 4 4,030 Jun-13-2021, 09:44 AM
Last Post: Gribouillis
  Writing unit test results into a text file ateestructural 3 4,787 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Help with dice roll program kraco 4 2,130 Sep-22-2020, 02:06 PM
Last Post: kraco
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,147 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,576 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn
  Remove function and unit test ftg 5 3,589 Jan-07-2020, 03:10 PM
Last Post: ndc85430
  Odd Unit Test Behavior ichabod801 3 2,617 Jan-02-2020, 03:34 PM
Last Post: ichabod801
  Define unit of measure of a number doug2019 3 2,418 Oct-15-2019, 03:43 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020