2017-11-10 65 views
1

我不能肯定,如果我失去了一些東西完全明顯,但每次我呼籲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 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 Crash Course(2016版)學習Python,如果這是任何參考。這個問題還沒有出現在我從中學到的其他課程中。

這是我已經試過:

我已經試過與give_raise(個體經營,salary_raise =無)方法擺弄周圍,並改變了該如何在工作情況下,我把事搞砸了的東西在內部有,但我不明白爲什麼會影響測試。

我試過刪除它,並重寫一遍(因爲它不是很多代碼),希望我只是忘記了一些愚蠢的東西,但如果我做了,那麼我第二次忘記了它。

如果這是一個非常簡單的修補程序,並且道歉,如果我已經格式化這個問題的方式有什麼問題,或者如果這不是這樣的問題的論壇提前道歉 - 這是我第一次發佈這裏。

+0

非常抱歉,我沒有將docstrings添加到我的功能中,以澄清他們的工作。 – LGrillos

+0

用「測試」前綴測試方法讓它們運行。 –

回答

1

測試方法需要名稱以test_開頭,單元測試運行者才能找到它們。

致電您的測試test_give_default_raisetest_give_custom_raise

+0

我知道這將是愚蠢的東西,我忘了做... 非常感謝你的幫助,無論如何,丹尼爾。 – LGrillos

0

EmployeeTest類(unittest.TestCase):應該有test_方法運行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() 
0

更改方法的名稱和它應該運行。

相關問題