举例告诉你,未开发完成的接口该如何测试

发表于:2021-4-13 09:40

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:测试安静    来源:51Testing软件测试网原创

  前言
  在做接口测试的过程中,接口还没有开发完成呢,领导就让介入测试,刚开始小伙伴们肯定会懵,接口都没有开发完成,怎么测试?有的状态不容易构造目前接口没有办法实现,这个时候怎么测试?这个时候就要引入新的知识点---mock。

  mock
  什么是mock?mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。在工作中如果在接口没有开发完成时,也可以进行来模拟接口的返回数据从而验证接口的正确性。

  mock需要场景
  工作中,为了节约项目时间,开发测试可以进行同步进行,当开发没有完成接口时,测试人员可以通过mock来模拟接口返回数据内容。
  模拟暂时无法实现的功能:比如真实的支付功能。
  测试覆盖内容,当我们执行接口想要模拟状态400、500的场景,这个时候也可以通过mock。

  python中的mock
  python中有多重执行mock的方法,其中mock模块在python3.3以上版本已经内置到单元测试unittest中,python3.3以下的版本需要通过pip install mock的方法进行安装。

  mock参数
  mock在使用过程中需要最常用的3个参数。
  name:表示mock的名称。
from unittest import mock
result = mock.Mock(name='mock的名称')
print(result)
# 结果:
<Mock name='mock的名称' id='2721150378120'>

  return_value:表示mock的值。

  side_effect:表示mock需要调用的对象(当使用return_value和side_effect值同时存在时,优先返回side_effect的值)。

  unittest.mock()
  这里安静举一个天气的例子进行实现,测试查询天气这个接口,最近上海都没有雪,那么我怎么去模拟雪这个场景?需要等到真正下雪的那天才可以吗?这里通过unittest.mock的方法。
import unittest
from unittest import mock

class Test01(unittest.TestCase):
    def weather(self):
        '''天气接口'''
        # result = {'result': "雪", 'status': '下雪了!'}
        pass
        
    def weather_result(self):
        '''模拟天气接口返回值'''
        result = Test01.weather(self)
        if result['result'] == '雪':
            print('下雪了!!!')
        elif result['result'] == '雨':
            print('下雨了!!!')
        elif result['result'] == '晴天':
            print('晴天!!!!')
        else:
            print('返回值错误!')
        return result['status']
        
    def test_01(self):
        '''模拟下雪天场景'''
        mock_xue_result = {'result': "雪", 'status': '下雪了!'}
        # 使用mock库进行模拟
        Test01.weather = mock.Mock(return_value=mock_xue_result)
        statues = Test01.weather_result(self)
        self.assertEqual(statues, '下雪了!')
        
    def test_02(self):
        '''模拟下雨天场景'''
        mock_yu_result = {'result': "雨", 'status': '下雨了!'}
        # 使用mock库进行模拟
        Test01.weather = mock.Mock(return_value=mock_yu_result)
        statues = Test01.weather_result(self)
        self.assertEqual(statues, '下雨了!')
        
if __name__ == '__main__':
    unittest.main()

  通过执行发现,我们想要的下雪天气已经模拟成功了,说明当查询天气接口返回为雪的时候,就代表会下雪了。这样就不用在等到下雪的时候进行测试这个接口了。

  mock.patch()
  mock.patch()是mock的一个装饰器方法,其中patch的值表示写入需要mock的对象,还拿天气举例子,这里的patch的参数就需要填写天气的接口,需要通过python调用的方式进行填写天气接口。
# w1.py

def weather():
 '''天气接口'''
    pass
def weather_result():
'''模拟天气接口返回值'''
    result = weather()
    if result['result'] == '雪':
        print('下雪了!!!')
    elif result['result'] == '雨':
        print('下雨了!!!')
    elif result['result'] == '晴天':
        print('晴天!!!!')
    else:
        print('返回值错误!')
    return result['status']

  通过mock.patch()装饰器进行来mock返回数据。
import unittest
from unittest import mock
# 导入接口文件
import w1
class Test01(unittest.TestCase):

    @mock.patch(target="w1.weather")
    def test_01(self, mock_login):
        '''下雪了'''
        mock_login.return_value={'result': "雪", 'status': '下雪了!'}
        statues = w1.weather_result()
        self.assertEqual(statues, '下雪了!')
    @mock.patch(target='w1.weather')
    def test_02(self,mock_login):
        '''下雨了!'''
        mock_login.return_value = {'result': "雨", 'status': '下雨了!'}
        statues = w1.weather_result()
        self.assertEqual(statues, '下雨了!')
if __name__ == '__main__':
    unittest.main()

  通过执行后发现,测试通过,已经成功的模拟了天气接口的返回值内容。

  pytest.mock()
  上面介绍的属于unittest中的mock,既然unittest中存在mock模块,那么pytest中也存在mock模块pytest-mock。
  安装:
pip install pytest-mock

  这里的mock和unittest的mock基本上都是一样的,唯一的区别在于pytest.mock需要导入需要mock对象的详细路径。
# weateher_r.py
class Mock_weather():
    def weather(self):
        '''天气接口'''
        pass
    def weather_result(self):
        '''模拟天气接口'''
        result = self.weather()
        if result['result'] == '雪':
            print('下雪了!!!')
        elif result['result'] == '雨':
            print('下雨了!!!')
        elif result['result'] == '晴天':
            print('晴天!!!!')
        else:
            print('返回值错误!')
        return result['status']

  先将需要模拟的天气接口,以及需要模拟的场景的代码写好,然后在进行遵循pytest的用例规范进行书写关于mock的测试用例
# test_01.py
import pytest
from test_01.weather_r import Mock_weather

def test_01(mocker):
    # 实例化
    p = Mock_weather()
    moke_value = {'result': "雪", 'status': '下雪了!'}
    # 通过object的方式进行查找需要mock的对象
    p.weather = mocker.patch.object(Mock_weather, "weather", return_value=moke_value)
    result =p.weather_result()
    assert result=='下雪了!'
    
def test_02(mocker):
    # 实例化
    product = Mock_weather()
    # Mock的返回值
    mock_value = {'result': "雨", 'status': '下雨了!'}
    # 第一个参数必须是模拟mock对象的完整路径
    product.weather = mocker.patch('test_01.weather_r.Mock_weather.weather',                                                   return_value=mock_value)
    result = product.weather_result()
    assert result=='下雨了!'
    
if __name__ == '__main__':
    pytest.main(['-vs'])

  通过上述代码,安静提供pytest中mock的2种方法:
  第一种中的第一个参数是通过object的方式进行查找关于Mock_weather的类,然后在找到下面的需要mock的对象方法名称,第2个参数表示mock的值。
  第二种方法中的第一个参数是通过完整的路径进行找到需要mock的对象,第2个参数是mock的值。
  通过执行发现,两种方法都是可以mock成功的。

  总结
  简单的通过一个小的例子介绍了如何在python中使用mock的方法。大家也可以将mock方法加入到我们需要用到的项目中。从而更加方面的完成自动化,以及覆盖更多的自动化测试用例。

      版权声明:本文出自51Testing会员投稿,51Testing软件测试网及相关内容提供者拥有内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号