Skip to content

Testing

Zapy files can be imported directly in your tests to perform assertions.

Request test hook

Under each zapy file, it's possible to implement multiple tests under a unittest.TestCase class.

For example

# test hook expects a unittest.TestCase class, cannot be async
import unittest

@ctx.hooks.test
class TestResponse(unittest.TestCase):
    def test_status_code(self):
        self.assertEqual(self.response.status_code, 200)

Integration test libraries

You can use any testing tool that support asynchronous for your tests.

pytest-asyncio

Example using pytest with the extension pytest-asyncio.

import pytest
from zapy import requests
import glob

@pytest.mark.asyncio
async def test_single(zapy_path):
    request = requests.from_path("request1.zapy")
    response = await request.send()
    response.json()

@pytest.mark.asyncio
@pytest.mark.parametrize("zapy_path", glob.glob("my_requests/*.zapy"))
async def test_parameterized(zapy_path):
    request = requests.from_path(zapy_path)
    response = await request.send()
    response.json()

Playwright

Example of a mixed scenario using pytest-playwright.

import pytest
import re
from playwright.async_api import async_playwright, expect
from zapy import requests

@pytest.mark.asyncio
async def test_has_title():
    async with async_playwright() as p:
        browser = await p.webkit.launch()
        page = await browser.new_page()
        await page.goto("https://playwright.dev/")

        # Expect a title "to contain" a substring.
        await expect(page).to_have_title(re.compile("Playwright"))
    request = requests.from_path('request1.zapy')
    response = await request.send()
    assert response.status_code == 200

CI/CD

You can implement your favorite CI/CD tool that runs python.

For example:

  • Jenkins
  • Travis CI
  • GitLab CI/CD

Read their documentation to run python unittest or pytest.