TestSuite套件可以添加很多个用例后运行,但是每个用例都需要调用addTest()函数。当测试用例很多时,需要把用例拆分,写到不同的.py文件中。
一个简单的测试项目目录如下:
测试用例放在同一个文件夹或不同目录下,此时可以使用unittest的TestLoader类加载测试用例。TestLoader类负责根据各种标准加载测试用例,并将它们返回给测试套件。unittest提供了共享的defaultTestLoader类,可调用discover()加载指定的case文件。
discover(start_dir, pattern = 'test*.py', top_level_dir = None)
1.start_dir:测试用例的目录或测试的模块名
2.pattern = ‘test*.py’:表示用例文件名的匹配原则,目录下以 "test" 开头的 ".py"文件
3.top_level_dir = None:测试模块的顶层目录,默认为None
runAllTest.py 如下:
# conding:utf-8
import unittest
test_dir = './test_case' #用例目录
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py') #加载所有的测试类并返回测试套件discover
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(discover) #运行测试套件discover
在 runAllTest.py 文件中使用TestLoader类的discover()方法加载 test_case/ 目录下的所有用例文件,然后通过TextTestRunner运行。
这样,只需运行runAllTest.py文件,即可自动执行所有的测试用例。
附test_case1.py 和 test_case2.py
test_case1.py 如下:
# coding:utf-8 from appium import webdriver import unittest, time class TestFixture(unittest.TestCase): @classmethod def setUpClass(cls): desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.4.2' desired_caps['deviceName'] = 'Android Emulator' desired_caps['appPackage'] = 'com.android.calculator2' desired_caps['appActivity'] = '.Calculator' cls.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) @classmethod def tearDownClass(cls): print('class end') cls.driver.quit()#加号运算case def test_add(self): self.driver.find_element_by_name("1").click() self.driver.find_element_by_name("+").click() self.driver.find_element_by_name("8").click() self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"=\")").click() self.driver.find_element_by_name("CLR").click() #减运算case def test_sub(self): self.driver.find_element_by_name("2").click() self.driver.find_element_by_xpath("//android.widget.Button[contains(@content-desc,'minus')]").click() self.driver.find_element_by_name("1").click() self.driver.find_element_by_name("=").click() self.driver.find_element_by_name("CLR").click() if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(TestFixture('test_add')) suite.addTest(TestFixture('test_sub')) runner = unittest.TextTestRunner() runner.run(suite)
test_case2.py 如下:
# coding:utf-8 from appium import webdriver import unittest, timeclass TestFixture(unittest.TestCase): @classmethod def setUpClass(cls): desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '4.4.2' desired_caps['deviceName'] = 'Android Emulator' desired_caps['appPackage'] = 'com.android.settings' desired_caps['appActivity'] = '.Settings' cls.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) @classmethod def tearDownClass(cls): cls.driver.quit() #开关蓝牙测试case def test_bluetooth(self): driver = self.driver driver.find_element_by_id("com.android.settings:id/switchWidget").click() time.sleep(1) driver.find_element_by_id("com.android.settings:id/switchWidget").click() driver.find_element_by_name("Bluetooth").click() time.sleep(1) driver.keyevent(4) #返回键 driver.keyevent(4) # 返回键 #开关data usage def test_dataUsage(self): driver = self.driver driver.find_element_by_name("Data usage").click() time.sleep(1) driver.find_element_by_class_name('android.widget.Switch') #开关data usage time.sleep(1) driver.find_element_by_class_name('android.widget.Switch') # 开关data usage driver.keyevent(4) # 返回键 driver.keyevent(4) # 返回键 if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(TestFixture('test_bluetooth')) suite.addTest(TestFixture('test_dataUsage')) runner = unittest.TextTestRunner() runner.run(suite)