Phần còn lại kiểm tra Python api

Khung tự động kiểm tra API Rest-API được xây dựng dựa trên ngôn ngữ Python bằng PyTest Framework

python rest-api test-automation pytest http-requests postman api-rest http-request-test pycharm-ide python-tests api-testing-framework rest-api-test-framework http-responce-code api-testing-python pytest-

  • Cập nhật12/06/2021
  • con trăn

bddwithTim / BDD-behave_petstore-swagger_API-testing_automation

Sao 4

  • Mã số
  • Vấn đề
  • Yêu cầu kéo

Thử nghiệm tự động hóa API BDD bằng Selenium Webdriver với Python và thơ để quản lý dự án và kiểm soát phiên bản

thơ dưa chuột Selenium-webdriver Selenium-python api-testing-python behavior-framework

  • Cập nhật ngày 28 tháng 8 năm 2022
  • con trăn

and-buk / api-test

Sao 3

  • Mã số
  • Vấn đề
  • Yêu cầu kéo

Đồ án thực hành REST API automation testing

yêu cầu api-testing-python page-object-pattern

  • Cập nhật17/10/2021
  • con trăn

salimsuprayogi / rest_api_testing_in_python

Sao 2

  • Mã số
  • Vấn đề
  • Yêu cầu kéo
  • thảo luận

Kiểm tra API REST bằng Python

python api-rest-api-testing-python testing-api rest-api-python

  • Cập nhật ngày 1 tháng 1 năm 2021
  • con trăn

mangeshrane / POC-Robot-Framework

Sao 0

  • Mã số
  • Vấn đề
  • Yêu cầu kéo

python robot poc api-rest Selenium-webdriver robotframework api-thử nghiệm từ khóa-thử nghiệm theo hướng kiểm tra Selenium-python api-testing-framework bdd-style-testing-framework api-testing-python Selenium-test

Hướng dẫn sau đây trình bày cách kiểm tra việc sử dụng API bên ngoài bằng các đối tượng mô phỏng Python

Tích hợp với ứng dụng của bên thứ ba là một cách tuyệt vời để mở rộng chức năng của sản phẩm của bạn

Tuy nhiên, giá trị gia tăng cũng đi kèm với trở ngại. Bạn không sở hữu thư viện bên ngoài, điều đó có nghĩa là bạn không thể kiểm soát các máy chủ lưu trữ thư viện đó, mã bao gồm logic của thư viện hoặc dữ liệu được truyền giữa thư viện đó và ứng dụng của bạn. Ngoài những vấn đề đó, người dùng liên tục thao tác dữ liệu thông qua các tương tác của họ với thư viện

Nếu bạn muốn nâng cao tiện ích cho ứng dụng của mình bằng API của bên thứ ba, thì bạn cần tự tin rằng hai hệ thống sẽ hoạt động tốt. Bạn cần kiểm tra giao diện của hai ứng dụng theo những cách có thể dự đoán được và bạn cần kiểm tra để thực thi trong môi trường được kiểm soát

Tiền thưởng miễn phí. Nhấp vào đây để tải xuống bản sao của Hướng dẫn "Ví dụ API REST" và nhận phần giới thiệu thực hành về các nguyên tắc API Python + REST với các ví dụ có thể thực hiện được

Thoạt nhìn, có vẻ như bạn không có bất kỳ quyền kiểm soát nào đối với ứng dụng của bên thứ ba. Nhiều người trong số họ không cung cấp máy chủ thử nghiệm. Bạn không thể kiểm tra dữ liệu trực tiếp và ngay cả khi bạn có thể, các kiểm tra sẽ trả về kết quả không đáng tin cậy vì dữ liệu được cập nhật thông qua sử dụng. Ngoài ra, bạn không bao giờ muốn các bài kiểm tra tự động của mình kết nối với máy chủ bên ngoài. Một lỗi từ phía họ có thể khiến quá trình phát triển của bạn bị đình trệ nếu việc phát hành mã của bạn phụ thuộc vào việc các bài kiểm tra của bạn có vượt qua hay không. May mắn thay, có một cách để kiểm tra việc triển khai API của bên thứ ba trong môi trường được kiểm soát mà không cần thực sự kết nối với nguồn dữ liệu bên ngoài. Giải pháp là giả mạo chức năng của mã bên ngoài bằng cách sử dụng thứ được gọi là giả

Mô hình giả là một đối tượng giả mạo mà bạn xây dựng để trông giống như dữ liệu thực. Bạn tráo đổi nó với đối tượng thực tế và đánh lừa hệ thống nghĩ rằng mô hình giả là hàng thật. Sử dụng mô phỏng làm tôi nhớ đến một bộ phim kinh điển trong đó người anh hùng tóm lấy một tên tay sai, mặc quân phục và bước vào hàng ngũ kẻ thù đang hành quân. Không ai để ý đến kẻ mạo danh và mọi người tiếp tục di chuyển—công việc vẫn như bình thường

Xác thực của bên thứ ba, chẳng hạn như OAuth, là ứng cử viên sáng giá để mô phỏng trong ứng dụng của bạn. OAuth yêu cầu ứng dụng của bạn giao tiếp với máy chủ bên ngoài, nó liên quan đến dữ liệu người dùng thực và ứng dụng của bạn dựa vào thành công của nó để có quyền truy cập vào các API của nó. Xác thực mô phỏng cho phép bạn kiểm tra hệ thống của mình với tư cách là người dùng được ủy quyền mà không phải trải qua quá trình trao đổi thông tin đăng nhập thực tế. Trong trường hợp này, bạn không muốn kiểm tra xem hệ thống của mình có xác thực thành công người dùng hay không;

GHI CHÚ. Hướng dẫn này sử dụng Python v3. 5. 1

Những bước đầu tiên

Bắt đầu bằng cách thiết lập một môi trường phát triển mới để giữ mã dự án của bạn. Tạo một môi trường ảo mới và sau đó cài đặt các thư viện sau

$ pip install nose requests

Dưới đây là tóm tắt nhanh về từng thư viện bạn đang cài đặt, trong trường hợp bạn chưa bao giờ gặp chúng

  • Thư viện giả được sử dụng để kiểm tra mã Python bằng cách thay thế các phần trong hệ thống của bạn bằng các đối tượng giả. GHI CHÚ. Thư viện
    # Third-party imports...
    from nose.tools import assert_true
    import requests
    
    
    def test_request_response[]:
        # Send a request to the API server and store the response.
        response = requests.get['//jsonplaceholder.typicode.com/todos']
    
        # Confirm that the request-response cycle completed successfully.
        assert_true[response.ok]
    
    3 là một phần của
    # Third-party imports...
    from nose.tools import assert_true
    import requests
    
    
    def test_request_response[]:
        # Send a request to the API server and store the response.
        response = requests.get['//jsonplaceholder.typicode.com/todos']
    
        # Confirm that the request-response cycle completed successfully.
        assert_true[response.ok]
    
    4 nếu bạn đang sử dụng Python 3. 3 hoặc cao hơn. Nếu bạn đang sử dụng phiên bản cũ hơn, vui lòng cài đặt thư viện giả lập backport
  • Thư viện mũi mở rộng mô-đun Python
    # Third-party imports...
    from nose.tools import assert_true
    import requests
    
    
    def test_request_response[]:
        # Send a request to the API server and store the response.
        response = requests.get['//jsonplaceholder.typicode.com/todos']
    
        # Confirm that the request-response cycle completed successfully.
        assert_true[response.ok]
    
    4 tích hợp để giúp kiểm tra dễ dàng hơn. Bạn có thể sử dụng
    # Third-party imports...
    from nose.tools import assert_true
    import requests
    
    
    def test_request_response[]:
        # Send a request to the API server and store the response.
        response = requests.get['//jsonplaceholder.typicode.com/todos']
    
        # Confirm that the request-response cycle completed successfully.
        assert_true[response.ok]
    
    4 hoặc các thư viện của bên thứ ba khác như pytest để đạt được kết quả tương tự, nhưng tôi thích các phương pháp xác nhận của mũi hơn
  • Thư viện yêu cầu đơn giản hóa rất nhiều lệnh gọi HTTP trong Python

Đối với hướng dẫn này, bạn sẽ giao tiếp với một API trực tuyến giả được tạo để thử nghiệm - Trình giữ chỗ JSON. Trước khi bạn viết bất kỳ bài kiểm tra nào, bạn cần biết những gì mong đợi từ API

Trước tiên, bạn nên mong đợi rằng API mà bạn đang nhắm mục tiêu thực sự trả về phản hồi khi bạn gửi yêu cầu. Xác nhận giả định này bằng cách gọi điểm cuối bằng cURL

$ curl -X GET '//jsonplaceholder.typicode.com/todos'

Cuộc gọi này sẽ trả về một danh sách các mục việc cần làm được tuần tự hóa JSON. Hãy chú ý đến cấu trúc của dữ liệu việc cần làm trong phản hồi. Bạn sẽ thấy một danh sách các đối tượng với các khóa

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
7,
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
8,
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
9 và
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
0. Bây giờ bạn đã sẵn sàng để đưa ra giả định thứ hai của mình – bạn biết dữ liệu trông như thế nào. Điểm cuối API vẫn hoạt động và hoạt động. Bạn đã chứng minh điều đó bằng cách gọi nó từ dòng lệnh. Nay viết thử mũi để sau này khẳng định tuổi thọ của server. Giữ nó đơn giản. Bạn chỉ nên quan tâm đến việc liệu máy chủ có trả về phản hồi OK hay không

dự án/thử nghiệm/test_todos. py

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]

Chạy thử nghiệm và xem nó vượt qua

$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK

Loại bỏ các quảng cáo

Tái cấu trúc mã của bạn thành một dịch vụ

Rất có thể bạn sẽ gọi API bên ngoài nhiều lần trong suốt ứng dụng của mình. Ngoài ra, các lệnh gọi API đó có thể sẽ liên quan đến nhiều logic hơn là chỉ thực hiện một yêu cầu HTTP, chẳng hạn như xử lý dữ liệu, xử lý lỗi và lọc. Bạn nên rút mã ra khỏi bài kiểm tra của mình và cấu trúc lại nó thành một chức năng dịch vụ gói gọn tất cả logic dự kiến ​​đó

Viết lại bài kiểm tra của bạn để tham khảo chức năng dịch vụ và để kiểm tra logic mới

dự án/thử nghiệm/test_todos. py

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
2

Chạy thử nghiệm và xem nó thất bại, sau đó viết số lượng mã tối thiểu để nó vượt qua

dự án/dịch vụ. py

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
3

dự án/hằng số. py

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
4

Thử nghiệm đầu tiên mà bạn đã viết mong đợi một phản hồi được trả về với trạng thái OK. Bạn đã tái cấu trúc logic lập trình của mình thành một chức năng dịch vụ tự trả về phản hồi khi yêu cầu tới máy chủ thành công. Giá trị

$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
1 được trả về nếu yêu cầu không thành công. Bài kiểm tra hiện bao gồm một xác nhận để xác nhận rằng hàm không trả về
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
1

Lưu ý cách tôi đã hướng dẫn bạn tạo một tệp

$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
3 và sau đó tôi điền vào tệp đó một tệp
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
4. Hàm dịch vụ mở rộng
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
4 để tạo
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
6 và vì tất cả các điểm cuối API đều sử dụng cùng một cơ sở nên bạn có thể tiếp tục tạo những điểm mới mà không cần phải viết lại đoạn mã đó. Đặt
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
4 vào một tệp riêng biệt cho phép bạn chỉnh sửa nó ở một nơi, điều này sẽ hữu ích nếu nhiều mô-đun tham chiếu mã đó

Chạy thử nghiệm và xem nó vượt qua

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
2

Mô phỏng đầu tiên của bạn

Mã đang hoạt động như mong đợi. Bạn biết điều này bởi vì bạn có một bài kiểm tra vượt qua. Rất tiếc, bạn gặp sự cố–chức năng dịch vụ của bạn vẫn đang truy cập trực tiếp vào máy chủ bên ngoài. Khi bạn gọi

$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8, mã của bạn đang đưa ra yêu cầu tới điểm cuối API và trả về kết quả phụ thuộc vào việc máy chủ đó đang hoạt động. Ở đây, tôi sẽ trình bày cách tách logic lập trình của bạn khỏi thư viện bên ngoài thực tế bằng cách hoán đổi yêu cầu thực với yêu cầu giả trả về cùng một dữ liệu

dự án/thử nghiệm/test_todos. py

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
4

Lưu ý rằng tôi hoàn toàn không thay đổi chức năng dịch vụ. Phần duy nhất của mã mà tôi đã chỉnh sửa là bản thân bài kiểm tra. Đầu tiên, tôi nhập hàm

$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
9 từ thư viện
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
3. Tiếp theo, tôi đã sửa đổi hàm kiểm tra bằng hàm
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
9 làm công cụ trang trí, chuyển tham chiếu đến
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
22. Trong chính hàm đó, tôi đã truyền vào một tham số
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
23, sau đó trong phần thân của hàm kiểm tra, tôi đã thêm một dòng để đặt
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
24

Tuyệt vời. Vì vậy, những gì thực sự xảy ra bây giờ khi thử nghiệm được chạy? . Khi bạn gọi hàm

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
26, nó sẽ thực hiện một yêu cầu HTTP phía sau hậu trường và sau đó trả về một phản hồi HTTP ở dạng đối tượng
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
27. Hàm
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
28 tự giao tiếp với máy chủ bên ngoài, đó là lý do tại sao bạn cần nhắm mục tiêu nó. Bạn có nhớ hình ảnh anh hùng đổi chỗ cho kẻ thù khi mặc quân phục không?

Khi chức năng kiểm tra được chạy, nó sẽ tìm thấy mô-đun nơi thư viện

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
25 được khai báo,
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
31 và nó thay thế chức năng được nhắm mục tiêu,
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
26, bằng một mô hình. Thử nghiệm cũng yêu cầu mô phỏng hành xử theo cách chức năng dịch vụ mong đợi nó hoạt động. Nếu bạn nhìn vào
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8, bạn sẽ thấy rằng sự thành công của hàm phụ thuộc vào việc ________ 134 trả về ________ 135. Đó là những gì dòng
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
24 đang làm. Khi thuộc tính
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
37 được gọi trên mô hình giả, nó sẽ trả về
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
35 giống như đối tượng thực tế. Hàm
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 sẽ trả về phản hồi, đó là bản mô phỏng và bài kiểm tra sẽ vượt qua vì bản mô phỏng không phải là
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
1

Chạy thử nghiệm để xem nó vượt qua

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
7

Loại bỏ các quảng cáo

Các cách khác để vá

Sử dụng một trình trang trí chỉ là một trong nhiều cách để vá một chức năng bằng một mô hình. Trong ví dụ tiếp theo, tôi vá rõ ràng một chức năng trong một khối mã, sử dụng trình quản lý bối cảnh. Câu lệnh

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
41 vá một hàm được sử dụng bởi bất kỳ mã nào trong khối mã. Khi khối mã kết thúc, chức năng ban đầu được khôi phục. Câu lệnh
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
41 và trình trang trí hoàn thành cùng một mục tiêu. Cả hai phương pháp vá
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
43

dự án/thử nghiệm/test_todos. py

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
0

Chạy các bài kiểm tra để thấy rằng chúng vẫn vượt qua

Một cách khác để vá một chức năng là sử dụng trình vá. Ở đây, tôi xác định nguồn để vá, và sau đó tôi bắt đầu sử dụng bản mô phỏng một cách rõ ràng. Bản vá không dừng lại cho đến khi tôi yêu cầu hệ thống ngừng sử dụng bản mô phỏng một cách rõ ràng

dự án/thử nghiệm/test_todos. py

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
1

Chạy lại các bài kiểm tra để có được kết quả thành công tương tự

Bây giờ bạn đã thấy ba cách để vá một chức năng bằng mô hình, khi nào bạn nên sử dụng từng cách? . nó hoàn toàn phụ thuộc vào bạn. Mỗi phương pháp vá là hoàn toàn hợp lệ. Điều đó đang được nói, tôi nhận thấy rằng các mẫu mã hóa cụ thể hoạt động đặc biệt tốt với các phương pháp vá lỗi sau

  1. Sử dụng trình trang trí khi tất cả mã trong phần thân hàm thử nghiệm của bạn sử dụng mô hình
  2. Sử dụng trình quản lý bối cảnh khi một số mã trong chức năng thử nghiệm của bạn sử dụng mô hình và mã khác tham chiếu đến chức năng thực tế
  3. Sử dụng trình vá lỗi khi bạn cần bắt đầu và dừng mô phỏng rõ ràng một chức năng qua nhiều lần kiểm tra [e. g. các hàm
    # Third-party imports...
    from nose.tools import assert_true
    import requests
    
    
    def test_request_response[]:
        # Send a request to the API server and store the response.
        response = requests.get['//jsonplaceholder.typicode.com/todos']
    
        # Confirm that the request-response cycle completed successfully.
        assert_true[response.ok]
    
    44 và
    # Third-party imports...
    from nose.tools import assert_true
    import requests
    
    
    def test_request_response[]:
        # Send a request to the API server and store the response.
        response = requests.get['//jsonplaceholder.typicode.com/todos']
    
        # Confirm that the request-response cycle completed successfully.
        assert_true[response.ok]
    
    45 trong một lớp kiểm tra]

Tôi sử dụng từng phương pháp này trong hướng dẫn này và tôi sẽ làm nổi bật từng phương pháp khi tôi giới thiệu lần đầu tiên

Chế giễu hành vi dịch vụ hoàn chỉnh

Trong các ví dụ trước, bạn đã triển khai một mô hình giả cơ bản và kiểm tra một xác nhận đơn giản–liệu hàm

$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 có trả về
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
1 hay không. Hàm
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 gọi API bên ngoài và nhận phản hồi. Nếu cuộc gọi thành công, hàm sẽ trả về một đối tượng phản hồi, chứa danh sách các công việc cần làm được tuần tự hóa JSON. Nếu yêu cầu không thành công,
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 trả về
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
1. Trong ví dụ sau, tôi trình bày cách mô phỏng toàn bộ chức năng của
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8. Khi bắt đầu hướng dẫn này, lệnh gọi ban đầu mà bạn thực hiện với máy chủ bằng cách sử dụng cURL đã trả về một danh sách từ điển được tuần tự hóa JSON, đại diện cho các mục việc cần làm. Ví dụ này sẽ chỉ cho bạn cách giả định dữ liệu đó

Hãy nhớ cách hoạt động của

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
22. Bạn cung cấp cho nó một đường dẫn đến chức năng mà bạn muốn mô phỏng. Hàm được tìm thấy,
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
9 tạo một đối tượng
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
24 và hàm thực tạm thời được thay thế bằng hàm giả. Khi thử nghiệm gọi
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8, hàm sẽ sử dụng phương thức
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
23 giống như cách nó sẽ sử dụng phương thức thực tế của
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
28. Điều đó có nghĩa là nó gọi
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
23 giống như một hàm và mong đợi nó trả về một đối tượng phản hồi

Trong trường hợp này, đối tượng phản hồi là một đối tượng

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
27 của thư viện
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
25, có một số thuộc tính và phương thức. Bạn đã giả mạo một trong những thuộc tính đó,
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
37, trong ví dụ trước. Đối tượng
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
27 cũng có hàm
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
43 chuyển đổi nội dung chuỗi được tuần tự hóa JSON của nó thành kiểu dữ liệu Python [e. g. một
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
44 hoặc một
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
45]

dự án/thử nghiệm/test_todos. py

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
2

Tôi đã đề cập trong một ví dụ trước rằng khi bạn chạy hàm

$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 đã được vá bằng một mô hình giả, hàm này đã trả về một đối tượng giả là “phản hồi”. Bạn có thể đã nhận thấy một mô hình. bất cứ khi nào
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
47 được thêm vào một mô phỏng, mô hình đó sẽ được sửa đổi để chạy dưới dạng một hàm và theo mặc định, nó trả về một đối tượng mô phỏng khác. Trong ví dụ này, tôi đã làm rõ hơn một chút bằng cách khai báo rõ ràng đối tượng
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
24,
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
49.
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
70 phản chiếu
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
26 và
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
26 trả về một
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
27 trong khi đó
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
70 trả về một
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
24. Đối tượng
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
27 có thuộc tính
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
37, vì vậy bạn đã thêm thuộc tính
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
37 vào
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
24

Đối tượng

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
27 cũng có hàm
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
43, vì vậy tôi đã thêm
$ curl -X GET '//jsonplaceholder.typicode.com/todos'
02 vào
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
24 và nối thêm nó với một
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
47, vì nó sẽ được gọi giống như một hàm. Hàm
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
43 trả về một danh sách các đối tượng việc cần làm. Lưu ý rằng bài kiểm tra hiện bao gồm một xác nhận kiểm tra giá trị của
$ curl -X GET '//jsonplaceholder.typicode.com/todos'
06. Bạn muốn đảm bảo rằng hàm
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 trả về một danh sách các việc cần làm, giống như máy chủ thực tế. Cuối cùng, để hoàn thiện thử nghiệm cho
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8, tôi thêm thử nghiệm cho lỗi

Chạy các bài kiểm tra và xem chúng vượt qua

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
3

Loại bỏ các quảng cáo

Mocking chức năng tích hợp

Các ví dụ tôi đã cho bạn thấy khá đơn giản, và trong ví dụ tiếp theo, tôi sẽ thêm vào sự phức tạp. Hãy tưởng tượng một kịch bản trong đó bạn tạo một chức năng dịch vụ mới gọi

$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 và sau đó lọc các kết quả đó để chỉ trả về các mục việc cần làm đã được hoàn thành. Bạn có phải chế nhạo
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
26 một lần nữa không? . Hãy nhớ rằng, khi bạn mô phỏng một chức năng, bạn đang thay thế đối tượng thực bằng mô hình giả và bạn chỉ phải lo lắng về cách chức năng dịch vụ tương tác với mô hình đó. Trong trường hợp của
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8, bạn biết rằng nó không cần tham số và nó trả về một phản hồi với hàm
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
43 trả về một danh sách các đối tượng việc cần làm. Bạn không quan tâm những gì xảy ra dưới mui xe;

dự án/thử nghiệm/test_todos. py

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
4

Lưu ý rằng bây giờ tôi đang vá chức năng kiểm tra để tìm và thay thế

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
16 bằng một bản giả. Hàm giả sẽ trả về một đối tượng có hàm
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
43. Khi được gọi, hàm
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
43 sẽ trả về một danh sách các đối tượng việc cần làm. Tôi cũng thêm một khẳng định để xác nhận rằng hàm
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 thực sự được gọi là. Điều này hữu ích để thiết lập rằng khi chức năng dịch vụ truy cập API thực, chức năng
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 thực sẽ thực thi. Ở đây, tôi cũng bao gồm một bài kiểm tra để xác minh rằng nếu
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 trả về
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
1, thì hàm
$ curl -X GET '//jsonplaceholder.typicode.com/todos'
23 sẽ trả về một danh sách trống. Một lần nữa, tôi xác nhận rằng hàm
$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8 được gọi là

Viết các bài kiểm tra, chạy chúng để xem chúng có bị lỗi không, sau đó viết mã cần thiết để chúng vượt qua

dự án/dịch vụ. py

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
5

Các bài kiểm tra bây giờ vượt qua

Tái cấu trúc các bài kiểm tra để sử dụng các lớp

Bạn có thể nhận thấy rằng một số bài kiểm tra dường như thuộc về một nhóm. Bạn có hai bài kiểm tra trúng hàm

$ nosetests --verbosity=2 project
test_todos.test_request_response .. ok

----------------------------------------------------------------------
Ran 1 test in 9.270s

OK
8. Hai bài kiểm tra khác của bạn tập trung vào
$ curl -X GET '//jsonplaceholder.typicode.com/todos'
23. Bất cứ khi nào tôi bắt đầu nhận thấy xu hướng và sự tương đồng giữa các bài kiểm tra, tôi cấu trúc lại chúng thành một lớp kiểm tra. Tái cấu trúc này hoàn thành một số mục tiêu

  1. Di chuyển các chức năng kiểm tra phổ biến sang một lớp cho phép bạn dễ dàng kiểm tra chúng cùng nhau hơn theo nhóm. Bạn có thể ra lệnh cho mũi nhắm mục tiêu một danh sách các hàm, nhưng việc nhắm mục tiêu một lớp đơn lẻ sẽ dễ dàng hơn
  2. Các chức năng kiểm tra phổ biến thường yêu cầu các bước tương tự để tạo và hủy dữ liệu được sử dụng bởi mỗi lần kiểm tra. Các bước này có thể được gói gọn trong các hàm
    $ curl -X GET '//jsonplaceholder.typicode.com/todos'
    
    27 và
    $ curl -X GET '//jsonplaceholder.typicode.com/todos'
    
    28 tương ứng để thực thi mã ở các giai đoạn thích hợp
  3. Bạn có thể tạo các hàm tiện ích trên lớp để tái sử dụng logic được lặp lại giữa các hàm kiểm tra. Hãy tưởng tượng bạn phải gọi cùng một logic tạo dữ liệu trong từng chức năng riêng lẻ. thật là đau

Lưu ý rằng tôi sử dụng kỹ thuật patcher để mô phỏng các chức năng được nhắm mục tiêu trong các lớp kiểm tra. Như tôi đã đề cập trước đây, phương pháp vá lỗi này rất phù hợp để tạo một bản mô phỏng trải rộng trên một số chức năng. Mã trong phương pháp

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
28 khôi phục rõ ràng mã gốc khi kiểm tra kết thúc

dự án/thử nghiệm/test_todos. py

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
6

Chạy thử nghiệm. Mọi thứ sẽ trôi qua vì bạn đã không giới thiệu bất kỳ logic mới nào. Bạn chỉ di chuyển mã xung quanh

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
7

Kiểm tra các bản cập nhật cho dữ liệu API

Trong suốt hướng dẫn này, tôi đã trình bày cách mô phỏng dữ liệu do API của bên thứ ba trả về. Dữ liệu giả đó dựa trên giả định rằng dữ liệu thực sử dụng cùng một hợp đồng dữ liệu như những gì bạn đang giả mạo. Bước đầu tiên của bạn là thực hiện cuộc gọi đến API thực tế và ghi chú dữ liệu được trả về. Bạn có thể khá tự tin rằng cấu trúc của dữ liệu không thay đổi trong thời gian ngắn mà bạn đã làm việc thông qua các ví dụ này, tuy nhiên, bạn không nên tự tin rằng dữ liệu sẽ không thay đổi mãi mãi. Thư viện bên ngoài nào tốt được cập nhật thường xuyên. Mặc dù các nhà phát triển nhằm mục đích làm cho mã mới tương thích ngược, nhưng cuối cùng cũng đến lúc mã không được dùng nữa

Như bạn có thể tưởng tượng, việc dựa hoàn toàn vào dữ liệu giả mạo là rất nguy hiểm. Vì bạn đang kiểm tra mã của mình mà không giao tiếp với máy chủ thực, nên bạn có thể dễ dàng trở nên quá tự tin vào sức mạnh của các bài kiểm tra của mình. Khi đến lúc sử dụng ứng dụng của bạn với dữ liệu thực, mọi thứ sẽ sụp đổ. Nên sử dụng chiến lược sau để xác nhận rằng dữ liệu bạn đang mong đợi từ máy chủ khớp với dữ liệu mà bạn đang kiểm tra. Mục tiêu ở đây là so sánh cấu trúc dữ liệu [e. g. các khóa trong một đối tượng] thay vì dữ liệu thực tế

Lưu ý cách tôi đang sử dụng kỹ thuật vá trình quản lý bối cảnh. Ở đây, bạn cần gọi máy chủ thực và bạn cần thử riêng nó

dự án/thử nghiệm/test_todos. py

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
8

bài kiểm tra của bạn nên vượt qua. Cấu trúc dữ liệu giả định của bạn khớp với cấu trúc từ API thực tế

Loại bỏ các quảng cáo

Kịch bản thử nghiệm có điều kiện

Bây giờ bạn đã có một thử nghiệm để so sánh các hợp đồng dữ liệu thực tế với các hợp đồng giả định, bạn cần biết khi nào nên chạy nó. Thử nghiệm truy cập vào máy chủ thực không nên được tự động hóa vì lỗi không nhất thiết có nghĩa là mã của bạn kém. Bạn có thể không kết nối được với máy chủ thực tại thời điểm thực thi bộ thử nghiệm của mình vì hàng tá lý do nằm ngoài tầm kiểm soát của bạn. Chạy thử nghiệm này riêng biệt với các thử nghiệm tự động của bạn, nhưng cũng chạy thử nghiệm này khá thường xuyên. Một cách để bỏ qua kiểm tra một cách có chọn lọc là sử dụng biến môi trường làm công tắc. Trong ví dụ bên dưới, tất cả các thử nghiệm đều chạy trừ khi biến môi trường

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
30 được đặt thành
# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
35. Khi biến
$ curl -X GET '//jsonplaceholder.typicode.com/todos'
30 được bật, bất kỳ thử nghiệm nào với trình trang trí
$ curl -X GET '//jsonplaceholder.typicode.com/todos'
33 sẽ bị bỏ qua

dự án/thử nghiệm/test_todos. py

$ curl -X GET '//jsonplaceholder.typicode.com/todos'
9

dự án/hằng số. py

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
0

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
1

Chạy thử nghiệm và chú ý đến đầu ra. Một thử nghiệm đã bị bỏ qua và bảng điều khiển hiển thị thông báo, “Bỏ qua các thử nghiệm đã tấn công máy chủ API thực. " Xuất sắc

# Third-party imports...
from nose.tools import assert_true
import requests


def test_request_response[]:
    # Send a request to the API server and store the response.
    response = requests.get['//jsonplaceholder.typicode.com/todos']

    # Confirm that the request-response cycle completed successfully.
    assert_true[response.ok]
2

Bước tiếp theo

Tại thời điểm này, bạn đã biết cách kiểm tra khả năng tích hợp ứng dụng của mình với API của bên thứ ba bằng mô hình. Bây giờ bạn đã biết cách tiếp cận vấn đề, bạn có thể tiếp tục thực hành bằng cách viết các hàm dịch vụ dành cho các điểm cuối API khác trong Trình giữ chỗ JSON [e. g. bài đăng, nhận xét, người dùng]

Tiền thưởng miễn phí. Nhấp vào đây để tải xuống bản sao của Hướng dẫn "Ví dụ API REST" và nhận phần giới thiệu thực hành về các nguyên tắc API Python + REST với các ví dụ có thể thực hiện được

Cải thiện kỹ năng của bạn hơn nữa bằng cách kết nối ứng dụng của bạn với một thư viện bên ngoài thực sự như Google, Facebook hoặc Evernote và xem liệu bạn có thể viết các bài kiểm tra sử dụng mô phỏng hay không. Tiếp tục tạo mã sạch và đáng tin cậy, đồng thời theo dõi hướng dẫn tiếp theo, hướng dẫn này sẽ mô tả cách đưa thử nghiệm lên cấp độ tiếp theo với các máy chủ giả

Lấy mã từ repo

Đánh dấu là đã hoàn thành

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Gửi cho tôi thủ thuật Python »

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

Làm cách nào để thử một API REST trong Python?

Chế nhạo API bên ngoài bằng Python .
Những bước đầu tiên
Tái cấu trúc mã của bạn thành một dịch vụ
Mô phỏng đầu tiên của bạn
Các cách khác để vá
Chế giễu hành vi dịch vụ hoàn chỉnh
Mocking chức năng tích hợp
Tái cấu trúc các bài kiểm tra để sử dụng các lớp
Kiểm tra các bản cập nhật cho dữ liệu API

Làm cách nào để kiểm tra API REST của tôi?

Các bước để kiểm tra API RESTful .
Mở ứng dụng khách REST nâng cao. Cài đặt máy khách REST nâng cao. .
Nhập URL của API bạn muốn kiểm tra vào hộp văn bản
Chọn phương thức HTTP trong thử nghiệm API, ví dụ POST
Cung cấp Tiêu đề được đặt trong hộp văn bản Tiêu đề. .
Nhấp vào SỬ DỤNG BỘ NÀY
Cung cấp nội dung cơ thể. .
Gửi thông tin chi tiết để bắt đầu thử nghiệm

Thử nghiệm API có thể được thực hiện bởi Python không?

Giờ đây, bạn có thể thêm các trường hợp thử nghiệm và xác nhận của riêng mình để tự động hóa các trường hợp thử nghiệm API . Hy vọng bài viết này sẽ giúp bạn bắt đầu thử nghiệm tự động hóa API bằng Python.

Làm cách nào để kiểm tra API REST bằng pytest?

Mở tệp Python để tạo trường hợp thử nghiệm đơn giản cho việc sử dụng mô-đun pytest. Luồng thử nghiệm cơ bản là tạo một chức năng chính cần được kiểm tra và một chức năng kiểm tra có tên bắt đầu bằng từ viết tắt “kiểm tra”. Để chạy trường hợp thử nghiệm, hãy viết “py.

Chủ Đề