基本上使用是导入一个待测试文件
导入unittest库
定义一个测试类,里面定义以test开头的测试函数
然后写一条 unittest.main() 执行所有测试函数
下面是一个例子:
import unittest
import python_repos
class TestPythonRepos(unittest.TestCase):
def test1(self):
result = python_repos.r.status_code
# 判断请求状态码是否正确
self.assertEqual(result, 200, "请求码错误")
def test2(self):
result = len(python_repos.repo_dicts)
# 比较请求数量是否超过需要数量
self.assertLess(result, 33, "超过所期望的数据数量")
if __name__ == "__main__":
# 执行类中所有以test开头的函数,不需要实例化
unittest.main()