练习11.3_雇员_Python编程:从入门到实践(第3版)

编写一个名为Employee的类,其__init__()方法接受名、姓和年薪,并将它们都存储在

属性中。编写一个名为give_raise()的方法,它默认将年薪增加5000美元,同时能够接受其

他的年薪增加量。

为Employee类编写一个测试文件,其中包含两个测试函数:

test_give_default_raise()和test_give_custom_raise()。在不使用夹具的情况下编

写这两个测试,并确保它们都通过了。然后,编写一个夹具,以免在每个测试函数中都创建一

个Employee对象。重新运行测试,确认两个测试都通过了。

编写的Employee类:

employee.py

class Employee:
    def __init__(self, name, annual_salary):
        self.name = name
        self.annual_salary = annual_salary

    def give_raise(self, add_salary=5000):
        self.annual_salary += add_salary

运行Employee类:

employee_salary.py

from employee import Employee

name = 'Jack'
annual_salary = 1000000

employee_salary = Employee(name, annual_salary)
employee_salary.give_raise()
add_salary = int(input('salary:'))
annual_salary += add_salary
print(f'增加了{add_salary},现在年薪是{annual_salary}')
employee_salary.give_raise()

为Employee类编写的测试文件:

import pytest
import source.employee as employee


def test_give_default_raise():
    # 增加薪水为默认值
    salary_amount = employee.Employee('Jack', 55000)
    salary_amount.give_raise()
    assert salary_amount.annual_salary == 60000


def test_give_custom_raise():
    # 增加薪水为其他值
    salary_amount = employee.Employee('Jack', 55000)
    salary_amount.give_raise(10000)
    assert salary_amount.annual_salary == 65000

编写一个夹具:

import pytest
import source.employee as employee


@pytest.fixture
def salary_amount():
    salary_amount = employee.Employee('Jack', 55000)
    return salary_amount


def test_give_default_raise(salary_amount):
    # 增加薪水为默认值
    salary_amount.give_raise()
    assert salary_amount.annual_salary == 60000


def test_give_custom_raise(salary_amount):
    # 增加薪水为其他值
    salary_amount.give_raise(10000)
    assert salary_amount.annual_salary == 65000

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值