Python 接口自動化測試中的十個魔法方法
在Python中,魔法方法(也稱為特殊方法)是一些特殊命名的方法,它們允許你定制類的行為。雖然這些方法不直接應用于接口自動化測試,但它們可以用來增強測試框架的功能。
__init__ 方法
實際使用場景: 初始化測試環(huán)境。
import unittest
import requests
class TestAPI(unittest.TestCase):
def __init__(self, methodName='runTest'):
super().__init__(methodName)
self.base_url = 'http://api.example.com'
def test_get_users(self):
response = requests.get(self.base_url + '/users')
self.assertEqual(response.status_code, 200)
setUp 方法
實際使用場景: 設(shè)置每個測試方法前的準備工作。
import unittest
import requests
class TestAPI(unittest.TestCase):
def setUp(self):
self.base_url = 'http://api.example.com'
self.headers = {'Content-Type': 'application/json'}
def test_get_users(self):
response = requests.get(self.base_url + '/users', headers=self.headers)
self.assertEqual(response.status_code, 200)
tearDown 方法
實際使用場景: 清理每個測試方法后的資源。
import unittest
import requests
class TestAPI(unittest.TestCase):
def setUp(self):
self.base_url = 'http://api.example.com'
self.headers = {'Content-Type': 'application/json'}
def tearDown(self):
# 清理資源
pass
def test_get_users(self):
response = requests.get(self.base_url + '/users', headers=self.headers)
self.assertEqual(response.status_code, 200)
__str__ 方法
實際使用場景: 改變測試類的字符串表示形式,便于調(diào)試。
import unittest
import requests
class TestAPI(unittest.TestCase):
def __str__(self):
return "API Test Suite"
def setUp(self):
self.base_url = 'http://api.example.com'
self.headers = {'Content-Type': 'application/json'}
def test_get_users(self):
response = requests.get(self.base_url + '/users', headers=self.headers)
self.assertEqual(response.status_code, 200)
__repr__ 方法
實際使用場景: 改變測試類的表示形式,便于調(diào)試。
import unittest
import requests
class TestAPI(unittest.TestCase):
def __repr__(self):
return ""
def setUp(self):
self.base_url = 'http://api.example.com'
self.headers = {'Content-Type': 'application/json'}
def test_get_users(self):
response = requests.get(self.base_url + '/users', headers=self.headers)
self.assertEqual(response.status_code, 200)
__eq__ 方法
實際使用場景: 自定義對象的相等性比較,可用于測試對象的等價性。
import unittest
import requests
class Response:
def __init__(self, status_code):
self.status_code = status_code
def __eq__(self, other):
return self.status_code == other.status_code
class TestAPI(unittest.TestCase):
def setUp(self):
self.base_url = 'http://api.example.com'
self.headers = {'Content-Type': 'application/json'}
def test_get_users(self):
response = Response(200)
self.assertEqual(response, Response(200))
__lt__ 方法
實際使用場景: 自定義對象的小于比較,可用于排序或測試對象間的大小關(guān)系。
import unittest
import requests
class Response:
def __init__(self, status_code):
self.status_code = status_code
def __lt__(self, other):
return self.status_code < other.status_code
class TestAPI(unittest.TestCase):
def setUp(self):
self.base_url = 'http://api.example.com'
self.headers = {'Content-Type': 'application/json'}
def test_compare_responses(self):
response1 = Response(200)
response2 = Response(404)
self.assertTrue(response1 < response2)
__len__ 方法
實際使用場景: 自定義對象的長度,可用于測試數(shù)據(jù)結(jié)構(gòu)的大小。
import unittest
import requests
class ResponseList:
def __init__(self, responses):
self.responses = responses
def __len__(self):
return len(self.responses)
class TestAPI(unittest.TestCase):
def setUp(self):
self.base_url = 'http://api.example.com'
self.headers = {'Content-Type': 'application/json'}
def test_response_list_length(self):
response_list = ResponseList([Response(200), Response(404)])
self.assertEqual(len(response_list), 2)
__iter__ 方法
實際使用場景: 自定義迭代行為,可用于循環(huán)遍歷對象集合。
import unittest
import requests
class ResponseList:
def __init__(self, responses):
self.responses = responses
def __iter__(self):
return iter(self.responses)
class TestAPI(unittest.TestCase):
def setUp(self):
self.base_url = 'http://api.example.com'
self.headers = {'Content-Type': 'application/json'}
def test_response_list_iteration(self):
response_list = ResponseList([Response(200), Response(404)])
for response in response_list:
self.assertGreaterEqual(response.status_code, 200)
__getitem__ 方法
實際使用場景: 自定義索引訪問行為,可用于通過索引訪問對象集合。
import unittest
import requests
class ResponseList:
def __init__(self, responses):
self.responses = responses
def __getitem__(self, index):
return self.responses[index]
class TestAPI(unittest.TestCase):
def setUp(self):
self.base_url = 'http://api.example.com'
self.headers = {'Content-Type': 'application/json'}
def test_response_list_index_access(self):
response_list = ResponseList([Response(200), Response(404)])
self.assertEqual(response_list[0].status_code, 200)