28. Python編寫自動化測試用例

接口文檔已經提供了,requests庫、unittest單元測試框架也已經介紹過,筆者相信讀者朋友已經能夠獨立編寫接口自動化測試用例了。可是有一些細節,咱們須要聊一下。好比咱們寫登陸接口測試用例,用戶名爲空、密碼爲空、用戶名錯誤、密碼錯誤、用戶名密碼都正確,假設咱們只編寫這幾條測試用例,咱們通常的作法以下代碼所示:json

# -*- coding: utf-8 -*-

import requests
import unittest
from nose_parameterized import parameterized


class BlogTest(unittest.TestCase):

    def setUp(self):
        self.url = "http://127.0.0.1:8000/login"
        self.username = 'admin'
        self.password = 'admin111111'

    def tearDown(self):
        pass

    def test_username_null(self):
        """用戶名爲空"""
        payload = {'username': '', 'password': self.password}
        self.result = requests.post(self.url, payload).json()
        # print(self.result)
        self.assertEqual(self.result['status'], 0)
        self.assertEqual(self.result['message'], 'username or password null')

    def test_password_null(self):
        """密碼爲空"""
        payload = {'username': self.username, 'password': ''}
        self.result = requests.post(self.url, payload).json()
        # print(self.result)
        self.assertEqual(self.result['status'], 0)
        self.assertEqual(self.result['message'], 'username or password null')

    def test_username_error(self):
        """用戶名錯誤"""
        payload = {'username': 'admin1', 'password': self.password}
        self.result = requests.post(self.url, payload).json()
        # print(self.result)
        self.assertEqual(self.result['status'], 0)
        self.assertEqual(self.result['message'], 'username or password error')

    def test_password_error(self):
        """密碼錯誤"""
        payload = {'username': self.username, 'password': 'admin1111111'}
        self.result = requests.post(self.url, payload).json()
        # print(self.result)
        self.assertEqual(self.result['status'], 0)
        self.assertEqual(self.result['message'], 'username or password error')

    def test_login_success(self):
        """用戶名密碼正確"""
        payload = {'username': self.username, 'password': self.password}
        self.result = requests.post(self.url, payload).json()
        # print(self.result)
        self.assertEqual(self.result['status'], 1)
        self.assertEqual(self.result['message'], 'login success')


if __name__ == '__main__':
    unittest.main()

歡迎關注微信公衆號:軟件測試汪。軟件測試交流羣:809111560微信

相關文章
相關標籤/搜索