1.继承类
import unittest
class TestApi(unittest.TestCase):
def test_01_login(self):
print("测试登陆")
def test_02_login(self):
print("测试注册")
2.测试套件的用法 unittest.main(defaultTest="suite")
import unittest
from test_apis.test_api import TestApi
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(TestApi("test_01_login"))
# 增加一组测试用例
testcases=[TestApi("test_01_login"),TestApi("test_02_login")]
suite.addTests(testcases)
unittest.main(defaultTest="suite")
3.通过加载器TestLoader加载
import unittest
from test_apis.test_api import TestApi
if __name__ == '__main__':
suite=unittest.defaultTestLoader.discover("./",pattern="test_*.py")
unittest.main(defaultTest="suite")
断言标题是否相等:
import unittest
from selenium import webdriver
class Test_Bai(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
driver=webdriver.Chrome()
driver.get("http://www.baidu.com")
title=driver.title
print(title)
return title
def test_assert_title(self):
self.assertEqual(self.setUpClass(),"百度一下,你就知道")
if __name__ == '__main__':
unittest.main
|