#!/usr/bin/env python3 """ The fraction_tester.py program should be placed in the same directory as the fraction.py class description. Running this program using: python fraction_tester.py will allow this tester to import the fraction package and attempt to create and manipulate Fraction objects. There are 8 tests that get run, including 1. creating a Fraction object 2. using the .getNum() and .getDen() methods 3. using the __str__ representation to print a string of the Fraction 4. adding one fraction to another 5. subtracting one fraction from another 6. multiplying two fractions 7. dividing one fraction by another 8. checking the equality of two fractions Good luck! @author Richard White @version 2017-02-09 """ from fraction import * def main(): print("Testing the Fraction class") tests_passed = 0 try: f1 = Fraction(5,1) tests_passed += 1 print("Test passed: successfully created Fraction object.") except: print("Test failed: couldn't create Fraction object.") try: if f1.getNum() == 5 and f1.getDen() == 1: print("Test passed: getNum() and getDen() successful") tests_passed +=1 else: print("Test failed: getNum() and getDem() incorrectly implemented") except: print("Test failed: getNum() and getDen() not working") try: if f1.__str__() == "5/1": print("Test passed: string representation correct") tests_passed += 1 else: print("Test failed: __str__ found but incorrect result.") except: print("Test failed. No string representation.") try: f2 = Fraction(-2,3) f3 = Fraction(1,4) except: print("Couldn't create other fractions") try: f4 = Fraction(3,6) f5 = Fraction(1,4) if str(f4+f5) == "3/4": print("Test passed: 3/6 + 1/4 = 3/4") tests_passed += 1 else: print("Test failed: addition found, but incorrect result") print(str(f4),"+",str(f5),"=",str(f4 + f5)) except: print("Test failed: __add__ method not found or some other issue!") try: if str(f1-f2) == "17/3": print("Test passed: 5/1 - -2/3 = 17/3") tests_passed += 1 else: print("Test failed: subtraction found, but incorrect result") print(str(f1),"-",str(f2),"=",str(f1 - f2)) except: print("Test failed: __sub__ method not found") try: if str(f1 * f2) == "-10/3": print("Test passed: 5/1 * -2/3 = -10/3") tests_passed += 1 else: print("Test failed: multiplication found, but incorrect result") print(str(f1),"*",str(f2),"=",str(f1 * f2)) except: print("Test failed: __mul__ method not found") try: if str(f1 / f2) == "-15/2": print("Test passed: 5/1 / -2/3 = -15/2") tests_passed += 1 else: print("Test failed: division found, but incorrect result") print(str(f1),"/",str(f2),"=",str(f1 / f2)) except: print("Test failed: __truediv__ method not found") try: if Fraction(1,2) == Fraction(5,10): print("Test passed: 1/2 = 5/10") tests_passed += 1 else: print("Test failed: __eq__ found, but incorrect result") except: print("Test failed: __eq__ comparison not found") print("Tests passed = " + str(tests_passed) + "/8") if __name__ == "__main__": main()