Assertions

Assertions in Python

CSTesting for Python (C:\CSTesting-Python) exposes from cstesting import expect (cstesting/assertions.py). There is no browser.assertThat()—combine await on the browser/locator with expect(...) on plain values.

expect(actual) matchers

MatcherDescription
to_be(expected)Identity (is)
to_equal(expected)Deep / JSON-stable equality
to_be_truthy() / to_be_falsy()Boolean coercion
to_be_null() / to_be_defined() / to_be_undefined()None checks (undefined maps to None)
to_throw(message?)expect(fn).to_throw() — sync or async callable
to_be_greater_than(n) / to_be_less_than(n)Numeric
to_contain(item)Substring or list membership
to_have_length(n)Length
expect(x).not_.to_be(y)Negation (use not_not is reserved)

Page and element state

Use BrowserApi / LocatorApi async queries, then assert:

from cstesting import expect

visible = await browser.is_visible("h1")
expect(visible).to_be_truthy()

editable = await browser.locator("#email").is_editable()
expect(editable).to_be_truthy()

text = await browser.locator("h1").first().text_content()
expect(text).to_contain("Example")

title = await browser.evaluate("document.title")
expect(title).to_contain("Domain")

current = await browser.url()
expect("example.com" in current).to_be_truthy()

attr = await browser.locator("a").first().get_attribute("href")
expect(attr).to_be_defined()

API responses

For HTTP tests, assert on the chained response or on get_response() (see example/apitesting_test.py):

from cstesting import describe, it, expect, request

def _suite():
    it("status", lambda: (
        request.get("https://api.example.com/health")
        .expect_status(200)
    ))
    def raw():
        res = request.get("https://api.example.com/users/1")
        res.expect_status(200)
        r = res.get_response()
        expect(r.status).to_be(200)
    it("raw body", raw)

describe("API", _suite)

Playwright-style expect(locator)

Python CSTesting does not re-export Playwright’s auto-retrying expect. Use Playwright’s action timeouts and await browser.wait_for_selector(...) before reading state, then plain expect(...).

Usage

from cstesting import describe, it, expect

def _suite():
    it("math", lambda: expect(1 + 1).to_be(2))
    it("list", lambda: expect([1, 2]).to_contain(2))

describe("Unit", _suite)

Failures raise cstesting.AssertionError.