Skip to Content
Python GUI Programming with Tkinter
book

Python GUI Programming with Tkinter

by Alan D. Moore
May 2018
Beginner to intermediate content levelBeginner to intermediate
452 pages
11h 26m
English
Packt Publishing
Content preview from Python GUI Programming with Tkinter

A simple unit test

At its most basic, a unit test is just a short program that runs a unit of code under different conditions and compares its output against expected results.

Consider the following calculation class:

import randomclass MyCalc:    def __init__(self, a, b):        self.a = a        self.b = b    def add(self):        return self.a + self.b    def mod_divide(self):        if self.b == 0:            raise ValueError("Cannot divide by zero")        return (int(self.a / self.b), self.a % self.b)    def rand_between(self):        return ((random.random() * abs(self.a - self.b)) +         min(self.a, self.b))

This class is initialized with two numbers and can then perform a variety of arithmetic methods on them.

Let's create a naive test for this function as follows:

from mycalc import MyCalcmc1 = MyCalc(1, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python GUI Programming with Tkinter - Second Edition

Python GUI Programming with Tkinter - Second Edition

Alan D. Moore

Publisher Resources

ISBN: 9781788835886Supplemental Content