Pyjo.Test.Pyjo - Testing Pyjo

from Pyjo.Test import *

import Pyjo.Test.Pyjo

t = Pyjo.Test.Pyjo.new('MyApp')

# HTML/XML
t.get_ok('/welcome').status_is(200).text_is('div#message', 'Hello!')

# JSON
(
    t.post_ok('/search.json', form={'q': 'Python'})
    .status_is(200)
    .header_is('Server', 'Pyjoyment (Python)')
    .header_isnt('X-Bender', 'Bite my shiny metal ass!')
    .json_is('/results/4/title', 'Python rocks!')
    .json_like('/results/7/title', re.compile(r'Python'))
)

# WebSocket
t.websocket_ok('/echo').send_ok('hello').message_ok().message_is('echo: hello').finish_ok()

# Server-Sent Events (SSE)
(
    t.get_sse_ok('/events')
    .status_is(200)
    .sse_ok()
    .sse_type_is('message')
    .sse_text_is('hello pyjo')
    .sse_finish_ok()
)

done_testing()

Pyjo.Test.Pyjo is a test user agent based on Pyjo.UserAgent, it is usually used together with Pyjo.Test to test Pyjoyment applications. Just run your tests with Pyjo.Prove.

$ python -m Pyjo.Prove -l -v
$ python -m Pyjo.Prove -l -v t/foo.py

If it is not already defined, the PYJO_LOG_LEVEL environment variable will be set to trace or fatal, depending on the value of the PYJO_HARNESS_IS_VERBOSE environment variable, which python -m Pyjo.Prove -v sets, or without it of HARNESS_IS_VERBOSE, which Perl’s prove -v sets. And to make it easier to test HTTPS/WSS web services Pyjo.UserAgent.Pyjo_UserAgent.insecure will be activated by default for ua.

The end of a with block closes the connections and the servers of its ua.

A test of unittest.TestCase makes it once for its class, after Pyjo.Test.raise_failures(), with enterClassContext(), which closes it at the end of the class:

import unittest

import Pyjo.Test
import Pyjo.Test.Pyjo


def setUpModule():
    unittest.enterModuleContext(Pyjo.Test.raise_failures())


class TestMyApp(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.t = cls.enterClassContext(Pyjo.Test.Pyjo.new('MyApp'))

    def test_welcome(self):
        self.t.get_ok('/welcome').status_is(200).text_is('div#message', 'Hello!')

A request of a test blocks, as it runs the event loop until the response, so the test is a method of a unittest.TestCase: in the running event loop of a unittest.IsolatedAsyncioTestCase, the default reactor raises Blocking request in a running event loop.

See also

Pyjoyment, https://mojolicious.org.

Ported from Test::Mojo of Mojolicious 9.49.

Classes

class Pyjo.Test.Pyjo.Pyjo_Test_Pyjo(app: Any = None, config: dict[str, Any] | None = None, /)[source]

Pyjo.Test.Pyjo inherits all methods from Pyjo.Base and implements the following new ones.

property app: Any[source]
app = t.app
t.app = Pyjoyment.new()

Access application with Pyjo.UserAgent.Server.Pyjo_UserAgent_Server.app.

# Change log level
t.app.log.level = 'fatal'

# Test application directly
is_ok(t.app.defaults('foo'), 'bar', 'right value')
ok(t.app.routes.find('echo').is_websocket, 'WebSocket route')
c = t.app.build_controller()
ok(c.render(template='foo'), 'rendering was successful')
is_ok(c.res.code, 200, 'right status')
is_ok(c.res.body, b'Foo!', 'right content')

# Change application behavior
@t.app.hook('before_dispatch')
def before_dispatch(c):
    if c.req.url.path.contains('/user'):
        c.render(text='This request did not reach the router.')

t.get_ok('/user').status_is(200).content_like(re.compile(r'not reach the router'))

# Extract additional information
stash = {}
t.app.hook('after_dispatch', lambda c: stash.update(c.stash()))
t.get_ok('/hello').status_is(200)
is_ok(stash['foo'], 'bar', 'right value')
attr_is(selector: str, attr: str, value: Any, desc: str | None = None) → Self[source]
t = t.attr_is('img.cat', 'alt', 'Grumpy cat')
t = t.attr_is('img.cat', 'alt', 'Grumpy cat', 'right alt text')

Checks text content of attribute with Pyjo.DOM.Pyjo_DOM.attr() at the CSS selectors first matching HTML/XML element for exact match with Pyjo.DOM.Pyjo_DOM.at().

attr_isnt(selector: str, attr: str, value: Any, desc: str | None = None) → Self[source]
t = t.attr_isnt('img.cat', 'alt', 'Calm cat')
t = t.attr_isnt('img.cat', 'alt', 'Calm cat', 'different alt text')

Opposite of attr_is().

attr_like(selector: str, attr: str, regex: Any, desc: str | None = None) → Self[source]
t = t.attr_like('img.cat', 'alt', re.compile(r'Grumpy'))
t = t.attr_like('img.cat', 'alt', re.compile(r'Grumpy'), 'right alt text')

Checks text content of attribute with Pyjo.DOM.Pyjo_DOM.attr() at the CSS selectors first matching HTML/XML element for similar match with Pyjo.DOM.Pyjo_DOM.at().

attr_unlike(selector: str, attr: str, regex: Any, desc: str | None = None) → Self[source]
t = t.attr_unlike('img.cat', 'alt', re.compile(r'Calm'))
t = t.attr_unlike('img.cat', 'alt', re.compile(r'Calm'), 'different alt text')

Opposite of attr_like().

close() → None[source]
t.close()

Close the connections and the servers of ua, as the end of a with block does.

content_is(value: Any, desc: str | None = None) → Self[source]
t = t.content_is('working!')
t = t.content_is('working!', 'right content')

Check response content for exact match after retrieving it from Pyjo.Message.Pyjo_Message.text.

content_isnt(value: Any, desc: str | None = None) → Self[source]
t = t.content_isnt('working!')
t = t.content_isnt('working!', 'different content')

Opposite of content_is().

content_like(regex: Any, desc: str | None = None) → Self[source]
t = t.content_like(re.compile(r'working!'))
t = t.content_like(re.compile(r'working!'), 'right content')

Check response content for similar match after retrieving it from Pyjo.Message.Pyjo_Message.text.

content_type_is(type: str, desc: str | None = None) → Self[source]
t = t.content_type_is('text/html')
t = t.content_type_is('text/html', 'right content type')

Check response Content-Type header for exact match.

content_type_isnt(type: str, desc: str | None = None) → Self[source]
t = t.content_type_isnt('text/html')
t = t.content_type_isnt('text/html', 'different content type')

Opposite of content_type_is().

content_type_like(regex: Any, desc: str | None = None) → Self[source]
t = t.content_type_like(re.compile(r'text'))
t = t.content_type_like(re.compile(r'text'), 'right content type')

Check response Content-Type header for similar match.

content_type_unlike(regex: Any, desc: str | None = None) → Self[source]
t = t.content_type_unlike(re.compile(r'text'))
t = t.content_type_unlike(re.compile(r'text'), 'different content type')

Opposite of content_type_like().

content_unlike(regex: Any, desc: str | None = None) → Self[source]
t = t.content_unlike(re.compile(r'working!'))
t = t.content_unlike(re.compile(r'working!'), 'different content')

Opposite of content_like().

delete_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.delete_ok('http://example.com/foo')
t = t.delete_ok('/foo')
t = t.delete_ok('/foo', {'Accept': '*/*'}, 'Content!')
t = t.delete_ok('/foo', {'Accept': '*/*'}, form={'a': 'b'})
t = t.delete_ok('/foo', {'Accept': '*/*'}, json={'a': 'b'})

Perform a DELETE request and check for transport errors, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.delete(), except for the callback.

element_count_is(selector: str, count: int, desc: str | None = None) → Self[source]
t = t.element_count_is('div.foo[x=y]', 5)
t = t.element_count_is('html body div', 30, 'thirty elements')

Checks the number of HTML/XML elements matched by the CSS selector with Pyjo.DOM.Pyjo_DOM.find().

element_exists(selector: str, desc: str | None = None) → Self[source]
t = t.element_exists('div.foo[x=y]')
t = t.element_exists('html head title', 'has a title')

Checks for existence of the CSS selectors first matching HTML/XML element with Pyjo.DOM.Pyjo_DOM.at().

# Check attribute values
(
    t.get_ok('/login')
    .element_exists('label[for=email]')
    .element_exists('input[name=email][type=text][value*="example.com"]')
    .element_exists('label[for=pass]')
    .element_exists('input[name=pass][type=password]')
    .element_exists('input[type=submit][value]')
)
element_exists_not(selector: str, desc: str | None = None) → Self[source]
t = t.element_exists_not('div.foo[x=y]')
t = t.element_exists_not('html head title', 'has no title')

Opposite of element_exists().

finish_ok(*args: Any) → Self[source]
t = t.finish_ok()
t = t.finish_ok(1000)
t = t.finish_ok(1003, 'Cannot accept data!')

Close WebSocket connection gracefully.

finished_ok(code: int) → Self[source]
t = t.finished_ok(1000)

Wait for WebSocket connection to be closed gracefully and check status.

get_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.get_ok('http://example.com/foo')
t = t.get_ok('/foo')
t = t.get_ok('/foo', {'Accept': '*/*'}, 'Content!')
t = t.get_ok('/foo', {'Accept': '*/*'}, form={'a': 'b'})
t = t.get_ok('/foo', {'Accept': '*/*'}, json={'a': 'b'})

Perform a GET request and check for transport errors, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.get(), except for the callback.

# Run tests against remote host
t.get_ok('https://github.com').status_is(200)

# Use relative URL for request with Basic authentication
t.get_ok('//sri:secr3t@/secrets.json').status_is(200).json_is('/1/content', 'Pyjo rocks!')

# Run additional tests on the transaction
t.get_ok('/foo').status_is(200)
is_ok(t.tx.res.dom().at('input').val, 'whatever', 'right value')
get_sse_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.get_sse_ok('http://example.com/events')
t = t.get_sse_ok('/events')
t = t.get_sse_ok('/events', {'Accept': 'text/event-stream'}, 'Content!')
t = t.get_sse_ok('/events', {'Accept': 'text/event-stream'}, form={'a': 'b'})
t = t.get_sse_ok('/events', {'Accept': 'text/event-stream'}, json={'a': 'b'})

Perform a GET request to establish a Server-Sent Events (SSE) connection, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.get(), except for the callback. Note that this method is EXPERIMENTAL and may change without warning!

handler: Callable[[...], Any]
cb = t.handler
t.handler = lambda name, *args: ...

A callback to connect Pyjo.Test.Pyjo with Pyjo.Test, which gets the name of a function of Test::More, as is, and its arguments.

def handler(name, *args):
    return {'is': is_ok, 'like': like_ok, 'ok': ok}[name](*args)

t.handler = handler
head_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.head_ok('http://example.com/foo')
t = t.head_ok('/foo')
t = t.head_ok('/foo', {'Accept': '*/*'}, 'Content!')
t = t.head_ok('/foo', {'Accept': '*/*'}, form={'a': 'b'})
t = t.head_ok('/foo', {'Accept': '*/*'}, json={'a': 'b'})

Perform a HEAD request and check for transport errors, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.head(), except for the callback.

header_exists(name: str, desc: str | None = None) → Self[source]
t = t.header_exists('ETag')
t = t.header_exists('ETag', 'header exists')

Check if response header exists.

header_exists_not(name: str, desc: str | None = None) → Self[source]
t = t.header_exists_not('ETag')
t = t.header_exists_not('ETag', 'header is missing')

Opposite of header_exists().

header_is(name: str, value: Any, desc: str | None = None) → Self[source]
t = t.header_is('ETag', '"abc321"')
t = t.header_is('ETag', '"abc321"', 'right header')

Check response header for exact match.

header_isnt(name: str, value: Any, desc: str | None = None) → Self[source]
t = t.header_isnt('Etag', '"abc321"')
t = t.header_isnt('ETag', '"abc321"', 'different header')

Opposite of header_is().

header_like(name: str, regex: Any, desc: str | None = None) → Self[source]
t = t.header_like('ETag', re.compile(r'abc'))
t = t.header_like('ETag', re.compile(r'abc'), 'right header')

Check response header for similar match.

header_unlike(name: str, regex: Any, desc: str | None = None) → Self[source]
t = t.header_unlike('ETag', re.compile(r'abc'))
t = t.header_unlike('ETag', re.compile(r'abc'), 'different header')

Opposite of header_like().

json_has(p: str, desc: str | None = None) → Self[source]
t = t.json_has('/foo')
t = t.json_has('/minibar', 'has a minibar')

Check if JSON response contains a value that can be identified using the given JSON Pointer with Pyjo.JSON.Pointer.

json_hasnt(p: str, desc: str | None = None) → Self[source]
t = t.json_hasnt('/foo')
t = t.json_hasnt('/minibar', 'no minibar')

Opposite of json_has().

json_is(*args: Any) → Self[source]
t = t.json_is({'foo': [1, 2, 3]})
t = t.json_is('/foo', [1, 2, 3])
t = t.json_is('/foo/1', 2, 'right value')

Check the value extracted from JSON response using the given JSON Pointer with Pyjo.JSON.Pointer, which defaults to the root value if it is omitted.

# Use an empty JSON Pointer to test the whole JSON response with a test description
t.json_is('', {'foo': [1, 2, 3]}, 'right object')
json_like(p: str, regex: Any, desc: str | None = None) → Self[source]
t = t.json_like('/foo/1', re.compile(r'^\d+$'))
t = t.json_like('/foo/1', re.compile(r'^\d+$'), 'right value')

Check the value extracted from JSON response using the given JSON Pointer with Pyjo.JSON.Pointer for similar match.

json_message_has(p: str, desc: str | None = None) → Self[source]
t = t.json_message_has('/foo')
t = t.json_message_has('/minibar', 'has a minibar')

Check if JSON WebSocket message contains a value that can be identified using the given JSON Pointer with Pyjo.JSON.Pointer.

json_message_hasnt(p: str, desc: str | None = None) → Self[source]
t = t.json_message_hasnt('/foo')
t = t.json_message_hasnt('/minibar', 'no minibar')

Opposite of json_message_has().

json_message_is(*args: Any) → Self[source]
t = t.json_message_is({'foo': [1, 2, 3]})
t = t.json_message_is('/foo', [1, 2, 3])
t = t.json_message_is('/foo/1', 2, 'right value')

Check the value extracted from JSON WebSocket message using the given JSON Pointer with Pyjo.JSON.Pointer, which defaults to the root value if it is omitted.

json_message_like(p: str, regex: Any, desc: str | None = None) → Self[source]
t = t.json_message_like('/foo/1', re.compile(r'^\d+$'))
t = t.json_message_like('/foo/1', re.compile(r'^\d+$'), 'right value')

Check the value extracted from JSON WebSocket message using the given JSON Pointer with Pyjo.JSON.Pointer for similar match.

json_message_unlike(p: str, regex: Any, desc: str | None = None) → Self[source]
t = t.json_message_unlike('/foo/1', re.compile(r'^\d+$'))
t = t.json_message_unlike('/foo/1', re.compile(r'^\d+$'), 'different value')

Opposite of json_message_like().

json_unlike(p: str, regex: Any, desc: str | None = None) → Self[source]
t = t.json_unlike('/foo/1', re.compile(r'^\d+$'))
t = t.json_unlike('/foo/1', re.compile(r'^\d+$'), 'different value')

Opposite of json_like().

message: list[Any] | None
msg = t.message
t.message = ['text', b'bytes']

Current WebSocket message represented as a list containing the frame type and payload.

# More specific tests
from Pyjo.JSON import decode_json

hash = decode_json(t.message[1])
is_ok(type(hash), dict, 'right reference')
is_ok(hash['foo'], 'bar', 'right value')

# Test custom message
t.message = ['binary', b'bytes']
t.json_message_has('/foo/bar').json_message_hasnt('/bar').json_message_is('/foo/baz', {'yada': [1, 2, 3]})
message_is(value: Any, desc: str | None = None) → Self[source]
t = t.message_is({'binary': b'bytes'})
t = t.message_is({'text': b'bytes'})
t = t.message_is('working!')
t = t.message_is('working!', 'right message')

Check WebSocket message for exact match.

message_isnt(value: Any, desc: str | None = None) → Self[source]
t = t.message_isnt({'binary': b'bytes'})
t = t.message_isnt({'text': b'bytes'})
t = t.message_isnt('working!')
t = t.message_isnt('working!', 'different message')

Opposite of message_is().

message_like(regex: Any, desc: str | None = None) → Self[source]
t = t.message_like({'binary': re.compile(rb'bytes')})
t = t.message_like({'text': re.compile(rb'bytes')})
t = t.message_like(re.compile(r'working!'))
t = t.message_like(re.compile(r'working!'), 'right message')

Check WebSocket message for similar match.

message_ok(desc: str | None = None) → Self[source]
t = t.message_ok()
t = t.message_ok('got a message')

Wait for next WebSocket message to arrive.

# Wait for message and perform multiple tests on it
(
    t.websocket_ok('/time')
    .message_ok()
    .message_like(re.compile(r'\d+'))
    .message_unlike(re.compile(r'\w+'))
    .finish_ok()
)
message_unlike(regex: Any, desc: str | None = None) → Self[source]
t = t.message_unlike({'binary': re.compile(rb'bytes')})
t = t.message_unlike({'text': re.compile(rb'bytes')})
t = t.message_unlike(re.compile(r'working!'))
t = t.message_unlike(re.compile(r'working!'), 'different message')

Opposite of message_like().

options_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.options_ok('http://example.com/foo')
t = t.options_ok('/foo')
t = t.options_ok('/foo', {'Accept': '*/*'}, 'Content!')
t = t.options_ok('/foo', {'Accept': '*/*'}, form={'a': 'b'})
t = t.options_ok('/foo', {'Accept': '*/*'}, json={'a': 'b'})

Perform a OPTIONS request and check for transport errors, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.options(), except for the callback.

otherwise(cb: Callable[[Self], Any]) → Self[source]
t = t.otherwise(lambda t: ...)

Execute callback if the value of success is false.

# Diagnostics
(
    t.get_ok('/bad')
    .otherwise(lambda t: diag('Must have been Glen!'))
    .status_is(200)
    .otherwise(lambda t: diag(t.tx.res.dom().at('title').text))
)
patch_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.patch_ok('http://example.com/foo')
t = t.patch_ok('/foo')
t = t.patch_ok('/foo', {'Accept': '*/*'}, 'Content!')
t = t.patch_ok('/foo', {'Accept': '*/*'}, form={'a': 'b'})
t = t.patch_ok('/foo', {'Accept': '*/*'}, json={'a': 'b'})

Perform a PATCH request and check for transport errors, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.patch(), except for the callback.

post_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.post_ok('http://example.com/foo')
t = t.post_ok('/foo')
t = t.post_ok('/foo', {'Accept': '*/*'}, 'Content!')
t = t.post_ok('/foo', {'Accept': '*/*'}, form={'a': 'b'})
t = t.post_ok('/foo', {'Accept': '*/*'}, json={'a': 'b'})

Perform a POST request and check for transport errors, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.post(), except for the callback.

# Test file upload
upload = {'foo': {'content': 'bar', 'filename': 'baz.txt'}}
t.post_ok('/upload', form=upload).status_is(200)

# Test JSON API
t.post_ok('/hello.json', json={'hello': 'world'}).status_is(200).json_is({'bye': 'world'})
post_sse_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.post_sse_ok('http://example.com/events')
t = t.post_sse_ok('/events')
t = t.post_sse_ok('/events', {'Accept': 'text/event-stream'}, 'Content!')
t = t.post_sse_ok('/events', {'Accept': 'text/event-stream'}, form={'a': 'b'})
t = t.post_sse_ok('/events', {'Accept': 'text/event-stream'}, json={'a': 'b'})

Perform a POST request to establish a Server-Sent Events (SSE) connection, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.post(), except for the callback. Note that this method is EXPERIMENTAL and may change without warning!

put_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.put_ok('http://example.com/foo')
t = t.put_ok('/foo')
t = t.put_ok('/foo', {'Accept': '*/*'}, 'Content!')
t = t.put_ok('/foo', {'Accept': '*/*'}, form={'a': 'b'})
t = t.put_ok('/foo', {'Accept': '*/*'}, json={'a': 'b'})

Perform a PUT request and check for transport errors, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.put(), except for the callback.

query_ok(url: Any, *args: Any, **kwargs: Any) → Self[source]
t = t.query_ok('http://example.com/foo')
t = t.query_ok('/foo')
t = t.query_ok('/foo', {'Accept': '*/*'}, 'Content!')
t = t.query_ok('/foo', {'Accept': '*/*'}, form={'a': 'b'})
t = t.query_ok('/foo', {'Accept': '*/*'}, json={'a': 'b'})

Perform a QUERY request and check for transport errors, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.query(), except for the callback.

request_ok(tx: Pyjo_Transaction_HTTP) → Self[source]
t = t.request_ok(Pyjo.Transaction.HTTP.new())

Perform request and check for transport errors.

# Request with custom method
tx = t.ua.build_tx('FOO', '/test.json', json={'foo': 1})
t.request_ok(tx).status_is(200).json_is({'success': 1})

# Request with custom cookie
tx = t.ua.build_tx('GET', '/account')
tx.req.cookies({'name': 'user', 'value': 'sri'})
t.request_ok(tx).status_is(200).text_is('head > title', 'Hello sri')

# Custom WebSocket handshake
tx = t.ua.build_websocket_tx('/foo')
tx.req.headers.remove('User-Agent')
t.request_ok(tx).message_ok().message_is('bar').finish_ok()
reset_session() → Self[source]
t = t.reset_session()

Reset user agent session.

send_ok(msg: Any, desc: str | None = None) → Self[source]
t = t.send_ok({'binary': b'bytes'})
t = t.send_ok({'text': b'bytes'})
t = t.send_ok({'json': {'test': [1, 2, 3]}})
t = t.send_ok([fin, rsv1, rsv2, rsv3, op, payload])
t = t.send_ok('chars')
t = t.send_ok('chars', 'sent successfully')

Send message or frame via WebSocket.

# Send JSON object as "Text" message
(
    t.websocket_ok('/echo.json')
    .send_ok({'json': {'test': 'I ♥ Pyjoyment!'}})
    .message_ok()
    .json_message_is('/test', 'I ♥ Pyjoyment!')
    .finish_ok()
)
sse: dict[str, Any] | None
event = t.sse
t.sse = {'type': 'message', 'text': 'working!'}

Current Server-Sent Event (SSE) represented as a dict. Note that this attribute is EXPERIMENTAL and may change without warning!

sse_finish_ok() → Self[source]
t = t.sse_finish_ok()

Close Server-Sent Events connection. Note that this method is EXPERIMENTAL and may change without warning!

sse_finished_ok() → Self[source]
t = t.sse_finished_ok()

Wait for Server-Sent Events connection to be closed. Note that this method is EXPERIMENTAL and may change without warning!

sse_id_is(id: Any, desc: str | None = None) → Self[source]
t = t.sse_id_is(123)
t = t.sse_id_is(123, 'right id')

Check Server-Sent Event id for exact match. Note that this method is EXPERIMENTAL and may change without warning!

sse_id_isnt(id: Any, desc: str | None = None) → Self[source]
t = t.sse_id_isnt(123)
t = t.sse_id_isnt(123, 'different id')

Opposite of sse_id_is(). Note that this method is EXPERIMENTAL and may change without warning!

sse_ok(desc: str | None = None) → Self[source]
t = t.sse_ok()
t = t.sse_ok('got an event')

Wait for next Server-Sent Event to arrive. Note that this method is EXPERIMENTAL and may change without warning!

sse_text_is(text: Any, desc: str | None = None) → Self[source]
t = t.sse_text_is('working!')
t = t.sse_text_is('working!', 'right text')

Check Server-Sent Event text for exact match. Note that this method is EXPERIMENTAL and may change without warning!

sse_text_isnt(text: Any, desc: str | None = None) → Self[source]
t = t.sse_text_isnt('working!')
t = t.sse_text_isnt('working!', 'different text')

Opposite of sse_text_is(). Note that this method is EXPERIMENTAL and may change without warning!

sse_text_like(regex: Any, desc: str | None = None) → Self[source]
t = t.sse_text_like(re.compile(r'working'))
t = t.sse_text_like(re.compile(r'working'), 'right text')

Check Server-Sent Event text for exact match. Note that this method is EXPERIMENTAL and may change without warning!

sse_text_unlike(regex: Any, desc: str | None = None) → Self[source]
t = t.sse_text_unlike(re.compile(r'working'))
t = t.sse_text_unlike(re.compile(r'working'), 'different text')

Opposite of sse_text_like(). Note that this method is EXPERIMENTAL and may change without warning!

sse_type_is(type: Any, desc: str | None = None) → Self[source]
t = t.sse_type_is('message')
t = t.sse_type_is('message', 'right type')

Check Server-Sent Event type for exact match. Note that this method is EXPERIMENTAL and may change without warning!

sse_type_isnt(type: Any, desc: str | None = None) → Self[source]
t = t.sse_type_isnt('message')
t = t.sse_type_isnt('message', 'different type')

Opposite of sse_type_is(). Note that this method is EXPERIMENTAL and may change without warning!

status_is(status: int, desc: str | None = None) → Self[source]
t = t.status_is(200)
t = t.status_is(200, 'right status')

Check response status for exact match.

status_isnt(status: int, desc: str | None = None) → Self[source]
t = t.status_isnt(200)
t = t.status_isnt(200, 'different status')

Opposite of status_is().

success: bool | None
boolean = t.success
t.success = boolean

True if the last test was successful.

# Build custom tests
def location_is(t, value, desc=''):
    desc = desc or f'Location: {value}'
    t.success = bool(is_ok(t.tx.res.headers.location, value, desc))
    return t

t.get_ok('/').status_is(302)
location_is(t, 'https://mojolicious.org').otherwise(lambda t: diag('Must have been Joel!'))
test(name: str, *args: Any) → Self[source]
t = t.test('is', 'first value', 'second value', 'right value')

Call Pyjo.Test functions through handler, by the names of the functions of Test::More (ok, is, isnt, like, unlike and is_deeply), used to implement Pyjo.Test.Pyjo extensions. The result will be stored in success.

text_is(selector: str, value: Any, desc: str | None = None) → Self[source]
t = t.text_is('div.foo[x=y]', 'Hello!')
t = t.text_is('html head title', 'Hello!', 'right title')

Checks text content of the CSS selectors first matching HTML/XML element for exact match with Pyjo.DOM.Pyjo_DOM.at().

text_isnt(selector: str, value: Any, desc: str | None = None) → Self[source]
t = t.text_isnt('div.foo[x=y]', 'Hello!')
t = t.text_isnt('html head title', 'Hello!', 'different title')

Opposite of text_is().

text_like(selector: str, regex: Any, desc: str | None = None) → Self[source]
t = t.text_like('div.foo[x=y]', re.compile(r'Hello'))
t = t.text_like('html head title', re.compile(r'Hello'), 'right title')

Checks text content of the CSS selectors first matching HTML/XML element for similar match with Pyjo.DOM.Pyjo_DOM.at().

text_unlike(selector: str, regex: Any, desc: str | None = None) → Self[source]
t = t.text_unlike('div.foo[x=y]', re.compile(r'Hello'))
t = t.text_unlike('html head title', re.compile(r'Hello'), 'different title')

Opposite of text_like().

tx: Pyjo_Transaction_HTTP | None
tx = t.tx
t.tx = Pyjo.Transaction.HTTP.new()

Current transaction, usually a Pyjo.Transaction.HTTP or Pyjo.Transaction.WebSocket object.

# More specific tests
is_ok(t.tx.res.json()['foo'], 'bar', 'right value')
ok(t.tx.res.content.is_multipart, 'multipart content')
is_ok(t.tx.previous.res.code, 302, 'right status')
property ua: Pyjo_UserAgent[source]
ua = t.ua
t.ua = Pyjo.UserAgent.new()

User agent used for testing, defaults to a Pyjo.UserAgent object.

# Allow redirects
t.ua.max_redirects = 10
t.get_ok('/redirect').status_is(200).content_like(re.compile(r'redirected'))

# Switch protocol from HTTP to HTTPS
t.ua.server.url('https')
t.get_ok('/secure').status_is(200).content_like(re.compile(r'secure'))

# Use absolute URL for request with Basic authentication
url = t.ua.server.url().set(userinfo='sri:secr3t').set(path='/secrets.json')
t.post_ok(url, json={'limit': 10}).status_is(200).json_is('/1/content', 'Pyjo rocks!')

# Customize all transactions (including followed redirects)
t.ua.on('start', lambda ua, tx: tx.req.headers.set(accept_language='en-US'))
t.get_ok('/hello').status_is(200).content_like(re.compile(r'Howdy'))
websocket_ok(url: Any, *args: Any) → Self[source]
t = t.websocket_ok('http://example.com/echo')
t = t.websocket_ok('/echo')
t = t.websocket_ok('/echo', {'DNT': 1}, ['v1.proto'])

Open a WebSocket connection with transparent handshake, takes the same arguments as Pyjo.UserAgent.Pyjo_UserAgent.websocket(), except for the callback.

# WebSocket with permessage-deflate compression
(
    t.websocket_ok('/', {'Sec-WebSocket-Extensions': 'permessage-deflate'})
    .send_ok('y' * 50000)
    .message_ok()
    .message_is('z' * 50000)
    .finish_ok()
)