在类python中使用固定装置

时间:2018-10-16 04:34:24

标签: python pytest fixtures

我有一个简单的BankAccount类程序,可以进行通常的存款,提款和结余。现在我想在测试中使用fixture,以便fixture创建对象实例,这样我就不必在测试函数中运行构造函数

class BankAccount:

    def __init__(self, name, ID, creation_date, amount):
        self.name = name
        self.ID = ID
        self.creation_date = creation_date
        self.amount = amount

    def deposit(self, amount):
        self.amount += amount
        print ("You Deposited: ", amount)
        print ("Your Balance is:", self.amount)

    def withdraw(self, amount):
        self.amount -= amount
        print ("You Withdrew", amount)
        print ("Your Balance is: ", self.amount)


    def view_balance(self):
        print ("Your Balance is: ", self.amount)

以下是我要执行的操作。它给我错误,对象BankAccount没有属性a_creation。我该如何完成

@pytest.fixture()
def a_creation():
    return ("whatever", "y7te", "12/1/2018", 2000)


def test_a_creation(a_creation):
    bk = BankAccount.a_creation
    test1 = bk.creation_date
    assert test1 == "12/1/2018"

我得到的错误是 TypeError: init ()缺少3个必需的位置参数:“ ID”,“ creation_date”和“ amount”

0 个答案:

没有答案
相关问题