Python 接口自動(dòng)化測(cè)試中的十個(gè)魔法方法
在Python中,魔法方法(也稱為特殊方法)是一些特殊命名的方法,它們?cè)试S你定制類的行為。雖然這些方法不直接應(yīng)用于接口自動(dòng)化測(cè)試,但它們可以用來增強(qiáng)測(cè)試框架的功能。
__init__ 方法
實(shí)際使用場(chǎng)景: 初始化測(cè)試環(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í)際使用場(chǎng)景: 設(shè)置每個(gè)測(cè)試方法前的準(zhǔn)備工作。
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 方法
實(shí)際使用場(chǎng)景: 清理每個(gè)測(cè)試方法后的資源。
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__ 方法
實(shí)際使用場(chǎng)景: 改變測(cè)試類的字符串表示形式,便于調(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__ 方法
實(shí)際使用場(chǎng)景: 改變測(cè)試類的表示形式,便于調(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__ 方法
實(shí)際使用場(chǎng)景: 自定義對(duì)象的相等性比較,可用于測(cè)試對(duì)象的等價(jià)性。
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__ 方法
實(shí)際使用場(chǎng)景: 自定義對(duì)象的小于比較,可用于排序或測(cè)試對(duì)象間的大小關(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í)際使用場(chǎng)景: 自定義對(duì)象的長(zhǎng)度,可用于測(cè)試數(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__ 方法
實(shí)際使用場(chǎng)景: 自定義迭代行為,可用于循環(huán)遍歷對(duì)象集合。
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__ 方法
實(shí)際使用場(chǎng)景: 自定義索引訪問行為,可用于通過索引訪問對(duì)象集合。
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)














 
 
 


















 
 
 
 