Python + Interface Requests introduction of automated testing framework Pytest

  Some time ago the company's business needs, ready to interface to automate the script before sort out the lengthy code before finishing into use cases one by one, so that the readability of the code becomes high, and the corresponding reduction in maintenance costs. We use is Pytest framework, on the introduction and benefits framework I will not go into here, and here to proceed directly from the actual use of the framework, we believe we have a more profound understanding of Pytest framework.

  Environment to build

  pip install pytest

  After installation directly

  pytest version

  practical use

  import pytest

  import requests

  import json

  import time

  @pytest.fixture()

  def feeds_get():

  feeds = requests.get(url='https://api.xxxx.xxxx.tv/feeds/feed/mix?devid=328ExxA-5954-4C29-9x48-AE0ExxB722B4&pageSize=9&uid=70192',

  headers={'version': '32'})

  JsonToDic = json.loads(feeds.text)

  return JsonToDic

  @pytest.fixture()

  def feed_detail(feeds_get):

  feedList = feeds_get['data']['list']

  return feedList

  @pytest.fixture()

  def feeds_parsing_date(feed_detail):

  crtTimeSortList = []

  for feed_count in range(0,9):

  feedList = (feed_detail[feed_count])

  crtTimeSortList.append ( feedList['crtTimeSort'] )

  return crtTimeSortList

  @pytest.fixture()

  def feeds_get_pidlist(feed_detail):

  pidlist = [] how much money Zhengzhou crowd http://www.hnmt120.com/

  for feed_count in range(0,9):

  feedList = (feed_detail[feed_count])

  pidlist.append(feedList['pid'] )

  return pidlist

  @pytest.fixture()

  def feeds_get_userid(feed_detail):

  userid = []

  for feed_count in range(0,9):

  feedList = (feed_detail[feed_count])

  userid.append(feedList['userid'])

  return userid

  @pytest.fixture()

  def feeds_get_column(feed_detail):

  columnlist = []

  for feed_count in range(0,9):

  feedList = (feed_detail[feed_count])

  columnlist.append(feedList['column_id'] )

  return columnlist

  # Server if the request fails

  def test_feeds_connect(feeds_get):

  assert feeds_get

  Whether there is a push account ID #

  def test_DeviceId(feeds_get):

  assert feeds_get['msg'] != 'recom result is not enough'

  # The number of requests to check whether the feed 9

  def test_feedcount(feed_detail):

  assert len(feed_detail) == 9

  # Check for duplicate content

  def test_repeatcontent(feeds_get_pidlist):

  assert len(feeds_get_pidlist) == len(set(feeds_get_pidlist))

  # Check for duplicate columns

  def test_repeatuser(feeds_get_column):

  assert len(feeds_get_column) == len(set(feeds_get_column))

  # Check for duplicate user

  def test_repeatuser(feeds_get_userid):

  assert len(feeds_get_userid) == len(set(feeds_get_userid))

  # Check whether the video article within three months of

  def test_feed_in90day(feeds_parsing_date):

  now_time = time.strftime("%Y%m%d%H%M%S", time.localtime())

  t = int(now_time)

  pass_time = t - 300000000

  for feed_count in range(0,9):

  assert int(feeds_parsing_date[feed_count]) > pass_time

  We can find that is not the case every automation can assert a written statement, the readability of the code is not such automation and improved maintainability also improved.


Guess you like

Origin blog.51cto.com/14503791/2455947