如何在python中测试类?
我使用spyder定义了这个类,为了测试这个类,我的老师会创建一个新的文件进行测试,他会检查每个方法是否返回了所需的结果,有人能告诉我怎么做吗如何在python中测试类?,python,class,testing,methods,spyder,Python,Class,Testing,Methods,Spyder,我使用spyder定义了这个类,为了测试这个类,我的老师会创建一个新的文件进行测试,他会检查每个方法是否返回了所需的结果,有人能告诉我怎么做吗 class Caminhos(): def __init__(self,vi,vf): self._vi = vi self._vf = vf self._caminhoI = [vi, vf] def primeiroV(self): retur
class Caminhos():
def __init__(self,vi,vf):
self._vi = vi
self._vf = vf
self._caminhoI = [vi, vf]
def primeiroV(self):
return self._caminhoI[0]
def ultimoV(self):
return self._caminhoI[-1]
def comprimento(self):
return len(self._caminhoI)
def acrescentaV(self, v):
self._caminhoI=self._caminhoI + [v]
def pertenceQ(self, v):
i = 0
b = False
while i < len(self._caminhoI) and not b:
if self._caminhoI[i] == v:
b = True
i = i + 1
return b
def verticePos(self, v):
#aqui preferia aplicar a pertenceQ ao V mas n sei bem a sintaxe
i = 0
b = False
while i < len(self._caminhoI) and not b:
if self._caminhoI[i] == v:
b = True
i = i + 1
if not b:
print("Erro, o vertice nao pertence ao caminho")
else:
i = 0
d = False
while i < len(self._caminhoI) and not d:
if self._caminhoI[i] == v:
d = True
i = i + 1
return i
def mostra(self):
return self._caminhoI
class Caminhos():
定义初始值(自、vi、vf):
自我。_vi=vi
自。_vf=vf
赛尔夫·卡米尼奥=[vi,vf]
def primeiroV(自我):
返回自我。\u caminhoI[0]
def ultimoV(自):
返回自我。卡米尼奥[-1]
def压缩机(自身):
回程透镜(自身)
def acrescentaV(自身,v):
self.\u caminhi=self.\u caminhi+[v]
def pertenceQ(自我,v):
i=0
b=错误
而我(自我)而不是b:
如果self._caminhoI[i]==v:
b=正确
i=i+1
返回b
def verticePos(自身,v):
#我更喜欢一份工作,一份工作
i=0
b=错误
而我(自我)而不是b:
如果self._caminhoI[i]==v:
b=正确
i=i+1
如果不是b:
打印(“Erro,o vertice nao pertence ao caminho”)
其他:
i=0
d=假
当我
定义类后,可以使用Spyder创建一个新文件来测试每个方法。创建一个新的python文件(您可以称之为Testing.py),确保它与前面定义的类的文件位于同一文件夹中,并且您可以执行下一个代码来测试您的类:
# Assuming your file is called "Caminhos" and it is in the same folder as this python script
# Import every method of your class
from Caminhos import *
# Instantiate class object using the built-in function __init__()
c1 = Caminhos(4,6)
# Use "." after the object to call each method
# See object using mostra() method
print(c1.mostra())
# Get first element using primeiroV() method
print("Primeiro elemento: {}".format(c1.primeiroV()))
# Get first element using ultimo() method
print("Ultimo elemento: {}".format(c1.ultimoV()))
# Get length using comprimento() method
print("Comprimento: {}".format(c1.comprimento()))
# Add element using comprimento() method
c1.acrescentaV(1)
print(c1.mostra())
# See if there is a number in object using pertenceQ() function
print(c1.pertenceQ(8))
print(c1.pertenceQ(4))
# See if there is a number in object using verticePos() function
print(c1.verticePos(4))
定义类之后,可以使用Spyder创建一个新文件来测试每个方法。创建一个新的python文件(您可以称之为Testing.py),确保它与前面定义的类的文件位于同一文件夹中,并且您可以执行下一个代码来测试您的类:
# Assuming your file is called "Caminhos" and it is in the same folder as this python script
# Import every method of your class
from Caminhos import *
# Instantiate class object using the built-in function __init__()
c1 = Caminhos(4,6)
# Use "." after the object to call each method
# See object using mostra() method
print(c1.mostra())
# Get first element using primeiroV() method
print("Primeiro elemento: {}".format(c1.primeiroV()))
# Get first element using ultimo() method
print("Ultimo elemento: {}".format(c1.ultimoV()))
# Get length using comprimento() method
print("Comprimento: {}".format(c1.comprimento()))
# Add element using comprimento() method
c1.acrescentaV(1)
print(c1.mostra())
# See if there is a number in object using pertenceQ() function
print(c1.pertenceQ(8))
print(c1.pertenceQ(4))
# See if there is a number in object using verticePos() function
print(c1.verticePos(4))