pytest错误(pytest和TDD新手)

2024-09-30 22:21:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我在测试实例变量的长度时遇到了一个问题。我经常会遇到这样的错误:

________________________________________ ERROR collecting test_Person.py _________________________________________
test_Person.py:7: in <module>
    person1 = Person("Tara", "Manderson", "F")
E   TypeError: 'module' object is not callable
    ------------------------------------------------ Captured stdout -------------------------------------------------
gender is:
F
last name is:
Manderson
first name is:
Tara
Ms. Tara Manderson is:
S
Ms. Tara Manderson
gender is:
M
last name is:
Murray
first name is:
Christopher
Mr. Christopher Murray is:
S
Mr. Christopher Murray
============================================ 1 error in 0.18 seconds =============================================

有人能解释和/或帮助我理解该怎么做吗?这是我的密码:

你知道吗个人.py 班级人员(对象):

    def __init__(self, first, last, sex):
        try:
            self.first = str(first)
            self.last = str(last)
            if (((sex=="M") or (sex=="F")) and (len(sex)==1)):
                self.sex = sex
            elif ((not(sex=="M") or not(sex=="F")) or not(len(sex)==1)):
                raise UserWarning("Invalid Input! Use \"M\" for male or \"F\" for female.")
            else:
                raise TypeError("Not a valid gender! Use \"M\" for male or \"F\" for female.")
            self.civilstat= "S"
        except TypeError:
            print ("invalid arguement error")


    def getSex(self):
        print ("gender is:")
        return self.sex


    def getLastName(self):
        print ("last name is:")
        return self.last


    def getFirstName(self):
        print ("first name is:")
        return self.first


    def getCivilStatus(self):
        print (self.formalName() + " is:")
        return self.civilstat


    def setStatus(self, stat):
        if (((self.civilstat=="M") or (self.civilstat=="D") or (self.civilstat=="S")) and (len(self.civilstat)==1)):
            self.civilstat= stat

        elif ((not(self.civilstat=="M") or not(self.civilstat=="F")) or not(len(self.civilstat)==1)):
            print ("not a valid status")

        else:
            print ("not a valid status")


    def setMarried(self, newLastName):
            if (self.sex == "F") and (newLastName == ""):
                raise ValueError("Please, what is her new last name? Re-enter her maiden name if she didn't change it.")

            elif (self.sex == "F") and (newLastName != ""):
                self.maiden= self.last
                self.last= newLastName
                self.civilstat= "M"

            elif (self.sex == "M") and (newLastName == ""):
                    self.civilstat= "M"

            elif (self.sex == "M") and (newLastName != ""):
                raise ValueError("Well, that's strange here. Please, leave his last name blank like \" \".")


    def setDivorced(self):
        if (self.civilstat != "M"):
            raise UserWarning("Wait! That person is not married.")
        elif (self.civilstat == "M"):
            self.civilstat= "D"
            self.last= self.maiden

    def formalName(self):
        if (self.sex== "M"):
            self.title= "Mr."

        elif (self.sex== "F"):
            if (self.civilstat== "M"):
                self.title= "Mrs."

            else:
                self.title= "Ms."

        return (self.title + " " + self.first + " " + self.last)


person1 = Person("Tara", "Manderson", "F")
person2 = Person("Christopher", "Murray", "M")
print (person1.getSex())
print (person1.getLastName())
print (person1.getFirstName())
print (person1.getCivilStatus())
print (person1.formalName())

print (person2.getSex())
print (person2.getLastName())
print (person2.getFirstName())
print (person2.getCivilStatus())
print (person2.formalName())

试验_个人.py你知道吗

    import pytest
    import Person


    person1 = Person("Tara", "Manderson", "F")

    @pytest.fixture
    def test_getSex():
            assert len(person1.getSex) == 1

Tags: ornameselfifisdefnotperson
2条回答

在测试项目中,我相信你必须

  • 在Person类前面加上模块名:人。人(…..)

或者

  • 将导入更改为“from Person import*”。在这种情况下,所有类都作为“local”导入

作为jcoppensmentioned,您需要修复导入。但是你的测试还有几个问题。你知道吗

你的测试应该是:

def test_getSex():
        assert len(person1.getSex()) == 1

注意getSex()-如果没有括号,则表示方法的长度,而不是返回的结果。你知道吗

作为一般提示,当您开始测试时,请使用print语句确保您正在测试您认为正在测试的内容。e、 g.打印出person1,将person1.getSex()赋给一个变量,并在断言它之前打印出来。你知道吗

而且在我看来,您的测试函数并不需要@pytest.fixture修饰符,因此可以删除它。你知道吗

相关问题 更多 >