Skip to content

Instantly share code, notes, and snippets.

@vicziani
Last active August 9, 2023 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicziani/81363a0a84c395b495774c96e99c116c to your computer and use it in GitHub Desktop.
Save vicziani/81363a0a84c395b495774c96e99c116c to your computer and use it in GitHub Desktop.
Python tesztelés

Az echo webszolgáltatás valami ilyesmit ad vissza, ha ez a kérés:

POST https://postman-echo.com/post

{
    "message": "Hello"
}
{
  "args": {},
  "data": {
    "message": "Hello"
  },
  "files": {},
  "form": {},
  "headers": {
    "x-forwarded-proto": "https",
    "x-forwarded-port": "443",
    "host": "postman-echo.com",
    "x-amzn-trace-id": "Root=1-64d3a8ad-7a6ec07a2c8469c61d1e29e1",
    "content-length": "20",
    "content-type": "application/json"
  },
  "json": {
    "message": "Hello"
  },
  "url": "https://postman-echo.com/post"
}

A conftest.py fájlra azért van szükség, hogy az URL-t egy helyen lehessen megadni, és környezeti változóból felül is lehessen írni.

A Visual Studio Code-ból indított tesztek nem írnak print esetén a konzolra. Ezért érdemes parancssorból indítani.

A -s kapcsolót kell használni, hogy ha nincs hiba, akkor is írjon konzolra. A -k kapcsolóval lehet megmondani, hogy mely teszt függvényt futtassa le.

pytest test_echo.py -k 'test_echo' -s
import os
import pytest
@pytest.fixture
def url():
default_url = "https://postman-echo.com/post"
result = os.environ.get("ECHO_URL", default_url)
return result
import requests
def test_echo(url):
# When
response = requests.post(url, json={"message": "Hello"}, timeout=2)
# Then
assert response.status_code == 200
print(response.text)
json_data = response.json()
print(json_data)
assert json_data["data"]["message"] == "Hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment