iOS自动化探索(四)自动化测试框架pytest安装和使用

 

自动化测试框架 - pytest

pytest是Python最流行的单元测试框架之一, 帮助更便捷的编写测试脚本, 并支持多种功能复杂的测试场景, 能用来做app测试也能用作函数测试

官方文档: 

  Could not find a version that satisfies the requirement pytest (from versions: ) No matching distribution found for pytest
复制代码

更新方式:

easy_install --upgrade pip

 

官方示例

准备一个test_sample.py, 内容如下:

复制代码
def inc(x):     return x + 1  def test_answer():     assert inc(3) == 5
复制代码

在文件所在目录执行:

pytest

这里我们做下说明:

pytest脚本都以test_xxx.py为文件名;

inc方法是我们定义的一个自增函数,该函数将传递进来的参数加1后返回;

test_answer是我们编写的一个测试函数,其中我们使用基本的断言语句assert来对结果进行验证,测试函数以test_xxx作为命名

 

执行结果如下:

复制代码
============================================================ test session starts ============================================================ platform darwin -- Python 2.7.15, pytest-4.1.0, py-1.7.0, pluggy-0.8.0 rootdir: /Users/jackey/Documents/iOS/code/iOS-Auto/Agent_Test, inifile: collected 1 item                                                                                                                              test_sample.py F                                                                                                                      [100%]  ================================================================= FAILURES ================================================================== ________________________________________________________________ test_answer ________________________________________________________________      def test_answer(): >       assert inc(3) == 5 E       assert 4 == 5 E        +  where 4 = inc(3)  test_sample.py:5: AssertionError ========================================================= 1 failed in 0.05 seconds ========================================================== (wda_python) bash-3.2$ 
复制代码

当执行到assert inc(3) == 5时,报错

 

执行pytest会在当前目录和子目录中寻找test_xx.py的测试文件,并进入到测试文件中寻找test_xx开头的测试函数开始执行

执行pytest -q  test_xxx.py是执行执行的脚本

 

在看一个例子,测试指定错误: (Assert that a certain exception is raised)

复制代码


                    
                
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信