python3单元测试_Python3之unittest单元测试框架

1. unittest 的定义

unittest 被称作 Python 版本的 JUnit ,有时也被叫做“PyUnit”。主要是用于 Python 语言程序的单元测试。

2. unittest 框架的 4 个重要概念

官方文档给出了 unittest 框架中 4 个重要的概念,介绍如下。

1)test fixture(测试固件)

一个 test fixture 代表一个或多个测试执行前的准备动作和测试结束后的清理动作 ,例如,创建数据库连接,启动服务进程,测试环境好清理或者关闭数据库连接等。

2)test case(测试用例)

一个 test case 就是一个最小测试单元,也就是一个完整的测试流程。针对一组特殊的输入进行特殊的验证与响应。通过继承 unittest 提供的测试基类(TestCase),可以创建新的测试用例。

3)test suit(测套件)

一个 test suit 就是一组测试用例,一组测试套件或者两者共同组成的集合。它的作用是将测试用例集合到一起,然后一次性执行集合中所有的测试用例。

4)test runner(测试运行器)

一个 test runner 由执行设定的测试用例和将测试结果提供给用户两部分功能组成。

3. 单元测试加载方法

两种单元测试的加载方法:

1)直接通过 unittest.main() 方法加载单元测试的测试模块,测试方法执行顺序按照方法名的字符串的ASCII码升序排序。

2)将所有的单元测试用例(Test Case)添加到测试套件(Test Suite)集合中,然后一次性加载所有测试对象。

4. 测试用例

TestCase 作为 unittest 单元测试框架中测试单元的运行实体,单元测试脚本编写员可以通过他派生出自定义的测试过程和方法。TestCase 子类从父类继承的几个特殊方法,在测试用例执行时均会被一次执行,

TestCase 类中定义的几个特殊方法如下。

1)setUp( ):每个测试方法运行前运行,测试前的初始化工作。

2)tearDown( ):每个测试方法运行结束后运行,测试后的清理工作。

3)setUpClass( ):所有测试方法运行前运行,单元测试前期准备,必须使用 @classmethod 装饰器进行装修饰,setUp( )函数之前执行,整个测试过程只执行一 次。

4)tearDownClass( ):所有测试方法结束后运行,单元测试后期清理,必须使用 @classmethod 装饰器进行装修饰,tearDown( )函数之后执行,整个测试过程只执行一次。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Appium unittest is a testing framework used to automate mobile application testing using Python programming language. It is built on top of the unittest module and provides a set of methods and assertions to test mobile applications. To use Python Appium unittest, you need to have Appium installed on your machine and a mobile device or emulator to test your application on. You also need to have the Appium Python Client installed in your Python environment. Here is an example of a Python Appium unittest script: ``` import unittest from appium import webdriver class TestApp(unittest.TestCase): def setUp(self): desired_caps = { 'platformName': 'Android', 'deviceName': 'Android Emulator', 'appPackage': 'com.example.myapp', 'appActivity': 'MainActivity' } self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) def tearDown(self): self.driver.quit() def test_login(self): email_field = self.driver.find_element_by_id('email') email_field.send_keys('[email protected]') password_field = self.driver.find_element_by_id('password') password_field.send_keys('password123') login_button = self.driver.find_element_by_id('login_button') login_button.click() assert 'Welcome' in self.driver.page_source if __name__ == '__main__': unittest.main() ``` In this example, we are testing a login functionality of an Android application. We first set up the desired capabilities for our test, which include the platform name, device name, app package, and app activity. We then initialize the Appium webdriver with these capabilities. In the `test_login` method, we find the email and password fields and enter test data. We then find the login button and click on it. Finally, we assert that the word 'Welcome' is present in the page source after successful login. To run this test, you can simply execute the script in your terminal using the command `python test_app.py`.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值