I've been completing challenge exercises from an introductory python textbook (python3), and for one of these I've made a python program that imports a class called Employee from another python file. They are both working properly. I've also made a program to run tests on the class. This test program produces the following errors:
EE
======================================================================
ERROR: test_give_custom_raise (__main__.TestEmployee)
----------------------------------------------------------------------
Traceback (most recent call last):
File "........python_projects/py_crash_course/py_crash_chap11/test_employee.py", line 19, in test_give_custom_raise
test_custom = self.employee_instance.give_raise(self.custom_raise)
AttributeError: 'TestEmployee' object has no attribute 'employee_instance'
======================================================================
ERROR: test_give_default_raise (__main__.TestEmployee)
----------------------------------------------------------------------
Traceback (most recent call last):
File "....python_projects/py_crash_course/py_crash_chap11/test_employee.py", line 14, in test_give_default_raise
employee_instance.give_raise(test_salary='1')
NameError: name 'employee_instance' is not defined
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (errors=2)
[Finished in 0.1s with exit code 1]
The code in the test program is as follows:
"""Tests the class: Employee contained in the file: employee_class.py"""
import unittest
from employee_class import Employee
class TestEmployee(unittest.TestCase):
"""Tests if the class: Employee works for both the default raise and a custom raise."""
"""Creates .... for use in all test methods."""
def SetUp(self):
self.custom_raise = 1000000
self.employee_instance = Employee()
self.employee_instance.give_raise(test_salary='1')
def test_give_default_raise(self):
self.assertEqual(TestEmployee.give_raise.salary, 5001)
def test_give_custom_raise(self):
test_custom = self.employee_instance.give_raise(self.custom_raise)
self.assertEqual(self.test_custom, self.custom_raise)
if __name__ == "__main__":
unittest.main()
The code in the program containing the class is as follows:
import json
class Employee:
""""""
def __init__(self, pay_raise=5000):
self.pay_raise = pay_raise
self.employee_details_list = []
self.saved_details = "saved_employee_details.json"
with open(self.saved_details) as s:
self.employee_details_dictionary = json.load(s)
def get_details(self):
self.first_name = input("Enter employee's first name: ").lower()
self.last_name = input("Enter employee's last name: ").lower()
not_yet = True
while not_yet:
try:
self.salary = int(input("Enter employee's salary (do not include the dollar sign or commas or spaces): "))
not_yet = False
except ValueError:
print("You cannot enter a dollar sign($), spaces, commas(,) or decimal point(.).")
print("You can only enter numerical digits.")
def make_list(self):
self.employee_details_list.append(self.first_name)
self.employee_details_list.append(self.salary)
def determine_pay_raise(self):
not_answered = True
y_n = ["y", "n"]
while not_answered:
custom_pay_raise_y_n = input("Will the employee get a custom pay raise? (y/n): ")
if custom_pay_raise_y_n not in y_n:
print("You can only enter 'y' for 'yes, or 'n' for 'no'.")
if custom_pay_raise_y_n == "n":
break
if custom_pay_raise_y_n == "y":
not_yet = True
while not_yet:
try:
self.pay_raise = int(input("Enter the amount of the pay raise (do not include the dollar sign,\n commas, spaces or decimal point): "))
not_answered = False
not_yet = False
except ValueError:
print("You cannot enter a dollar sign($), spaces, commas(,) or decimal point(.).")
def give_raise(self, test_value='', test_salary=''):
self.test_value = test_value
if self.test_value == '':
self.salary += self.pay_raise
return self.salary
self.employee_details_list[-1] = self.salary
if self.test_value !='':
self.salary += self.test_value
self.employee_details_list[-1] = self.salary
return self.salary
def add_to_dictionary(self):
self.employee_details_dictionary[self.last_name] = self.employee_details_list
def save_details(self):
with open(self.saved_details, "w") as s:
contents = self.employee_details_dictionary
json.dump(contents, s)
def print_new_salary(self):
print(f"{self.first_name.title()} {self.last_name.title()}'s new annual salary is ${self.salary}")
And the code in the main program is as follows:
from employee_class import Employee
keep_going = True
while keep_going:
IndividualEmployee = Employee()
IndividualEmployee.get_details()
IndividualEmployee.make_list()
IndividualEmployee.determine_pay_raise()
IndividualEmployee.give_raise()
IndividualEmployee.add_to_dictionary()
IndividualEmployee.save_details()
IndividualEmployee.print_new_salary()
y_n = ["y", "n"]
not_yet = True
while not_yet:
another_employee = input("\nDo you want to input information for another employee? (y/n): ")
if another_employee not in y_n:
print("You can only enter 'y' for 'yes', or 'n' for 'no'.\n")
if another_employee == "n":
keep_going = False
break
if another_employee == "y":
not_yet = False
print("\n")
I don't understand why the test program is producing these errors. In particular, in the test program, I feel like I have defined the variable 'employee_instance' in the SetUp() function in the test program. Why isn't it being recognised? How can I fix it?
Thanks!
Aucun commentaire:
Enregistrer un commentaire