无法理解如何使用 unittest 模块在 python 中为工作日编写测试用例
Cannot understand how to write a testcase for weekdays in python using unittest module
我在 python 中有一个函数,它将工作日作为参数,returns 一个 int 成功的工作日,我是 python 单元测试的新手,我想知道如何为此编写单元测试用例。这是函数:
def dayNameFromWeekday(weekday):
if weekday == "Monday":
return 0
if weekday == "Tuesday":
return 1
if weekday == "Wednesday":
return 2
if weekday == "Thursday":
return 3
if weekday == "Friday":
return 4
if weekday == "Saturday":
return 5
if weekday == "Sunday":
return 6
这是测试文件
class Utils(unittest.TestCase):
def test_dayNameFromWeekday(self, weekday):
if weekday == "Monday":
return 0
elif weekday == "Tuesday":
return 1
elif weekday == "Wednesday":
return 2
elif weekday == "Thursday":
return 3
elif weekday == "Friday":
return 4
elif weekday == "Saturday":
return 5
elif weekday == "Sunday":
return 6
else:
return -1 # Any error message
res = self.test_dayNameFromWeekday("Monday")
我不知道如何为此功能编写测试用例,请告诉我在单元测试中完成事情的方式python..
解决方法如下:
import unittest
class Utils(unittest.TestCase):
def test_dayNameFromWeekday(self, weekday):
if weekday == "Monday":
return 0
elif weekday == "Tuesday":
return 1
elif weekday == "Wednesday":
return 2
elif weekday == "Thursday":
return 3
elif weekday == "Friday":
return 4
elif weekday == "Saturday":
return 5
elif weekday == "Sunday":
return 6
else:
return -1 # Any error message
def runTest(self):
res = self.test_dayNameFromWeekday("Monday")
print(res)
res = Utils()
res.run()
我在 python 中有一个函数,它将工作日作为参数,returns 一个 int 成功的工作日,我是 python 单元测试的新手,我想知道如何为此编写单元测试用例。这是函数:
def dayNameFromWeekday(weekday):
if weekday == "Monday":
return 0
if weekday == "Tuesday":
return 1
if weekday == "Wednesday":
return 2
if weekday == "Thursday":
return 3
if weekday == "Friday":
return 4
if weekday == "Saturday":
return 5
if weekday == "Sunday":
return 6
这是测试文件
class Utils(unittest.TestCase):
def test_dayNameFromWeekday(self, weekday):
if weekday == "Monday":
return 0
elif weekday == "Tuesday":
return 1
elif weekday == "Wednesday":
return 2
elif weekday == "Thursday":
return 3
elif weekday == "Friday":
return 4
elif weekday == "Saturday":
return 5
elif weekday == "Sunday":
return 6
else:
return -1 # Any error message
res = self.test_dayNameFromWeekday("Monday")
我不知道如何为此功能编写测试用例,请告诉我在单元测试中完成事情的方式python..
解决方法如下:
import unittest
class Utils(unittest.TestCase):
def test_dayNameFromWeekday(self, weekday):
if weekday == "Monday":
return 0
elif weekday == "Tuesday":
return 1
elif weekday == "Wednesday":
return 2
elif weekday == "Thursday":
return 3
elif weekday == "Friday":
return 4
elif weekday == "Saturday":
return 5
elif weekday == "Sunday":
return 6
else:
return -1 # Any error message
def runTest(self):
res = self.test_dayNameFromWeekday("Monday")
print(res)
res = Utils()
res.run()