Python unittest不是';你不在这里工作吗?
我不确定是否遗漏了一些非常明显的内容,但每次调用unittest.main()时,我的命令行输出都会显示:Python unittest不是';你不在这里工作吗?,python,unit-testing,testing,Python,Unit Testing,Testing,我不确定是否遗漏了一些非常明显的内容,但每次调用unittest.main()时,我的命令行输出都会显示: Ran 0 tests in 0.000s 应该说,根据这里的代码,我已经运行了两个测试: import unittest from Chap11Lesson2 import Employee class EmployeeTest(unittest.TestCase): def setUp(self): """Setting up a variable
Ran 0 tests in 0.000s
应该说,根据这里的代码,我已经运行了两个测试:
import unittest
from Chap11Lesson2 import Employee
class EmployeeTest(unittest.TestCase):
def setUp(self):
"""Setting up a variable for use in the test methods"""
self.employee1 = Employee("Lucas", "Grillos", 20000)
def give_default_raise(self):
"""Testing to see if 5000 is added properly"""
money = 25000
self.employee1.give_raise()
self.assertEqual(self.employee1.salary, money)
def give_custom_raise(self):
"""Testing to see if 10000 is raised properly"""
money = 35000
self.employee1.give_raise(10000)
self.assertEqual(self.employee1.salary, money)
unittest.main()
下面是它测试的类:
class Employee():
def __init__(self, first_name, last_name, salary):
self.first_name = first_name
self.last_name = last_name
self.salary = salary
def give_raise(self, salary_raise = None):
if salary_raise:
self.salary = self.salary + salary_raise
else:
self.salary = self.salary + 5000
def print_salary(self):
print(self.salary)
我以前从未遇到过这样的问题,所以我不知道该怎么办。我正在从Eric Matthes的Python速成班(2016版)学习Python,如果有参考的话。这个问题在我从中学到的其他课程中也没有出现过
以下是我尝试过的:
我尝试过摆弄give_raise(self,salary_raise=None)方法,如果我在内部搞砸了,我会改变它的工作方式,但我不明白为什么这会影响测试
我曾经尝试过删除它并再次重写它(因为它不是很多代码),希望我只是忘记了一些愚蠢的事情,但是如果我忘记了,那么我又忘记了它
如果这是一个非常简单的修复,请提前道歉,如果我格式化此问题的方式有任何问题,或者如果这不是一个讨论此类问题的论坛,请道歉——这是我第一次在这里发布。测试方法的名称必须以
Test\uu
开头,unittest runner才能找到它们
调用您的测试test\u-give\u-default\u-raise
和test\u-give\u-custom\u-raise
类EmployeeTest(unittest.TestCase):,应该有测试方法来运行unittest模块。
所以试试这个:
import unittest
from Chap11Lesson2 import Employee
class EmployeeTest(unittest.TestCase):
def setUp(self):
"""Setting up a variable for use in the test methods"""
self.employee1 = Employee("Lucas", "Grillos", 20000)
def test_give_default_raise(self):
"""Testing to see if 5000 is added properly"""
money = 25000
self.employee1.give_raise()
self.assertEqual(self.employee1.salary, money)
def test_give_custom_raise(self):
"""Testing to see if 10000 is raised properly"""
money = 35000
self.employee1.give_raise(10000)
self.assertEqual(self.employee1.salary, money)
unittest.main()
更改您的方法名称,使其在测试方法前加上“test”,然后应该运行。很抱歉,我没有在函数中添加docstring以澄清它们的作用。请在您的测试方法前加上“test”以使其运行。我知道这将是一件愚蠢的事情,我忘了做。。。谢谢你在任何情况下的帮助,丹尼尔。