Testing helpers for Kyoukai.


TestKyoukai(*args, …) A special subclass that allows you to easily test your Kyoukai-based app.
class kyoukai.testing._TestingBpCtxManager(app: kyoukai.testing.TestKyoukai)[source]

Bases: object

A context manager that is returned from testing_bp(). When entered, this will produce a new Blueprint object, that is then set onto the test application as the root blueprint.

After exiting, it will automatically restore the old root Blueprint onto the application, allowing complete isolation of individual test routes away from eachother.

class kyoukai.testing.TestKyoukai(*args, base_context: asphalt.core.context.Context = None, **kwargs)[source]


A special subclass that allows you to easily test your Kyoukai-based app.

Parameters:base_context (Optional[Context]) – The base context to use for all request testing.
classmethod wrap_existing_app(base_context=None)[source]

Wraps an existing app in a test frame.

This allows easy usage of writing unit tests:

kyk = Kyoukai("my_app")

testing = TestKyoukai.wrap_existing_app(other_app)
# use testing as you would normally
  • other_app (Kyoukai) –

    The application object to wrap. Internally, this creates a new instance of ourselves, then sets the process_request of the subclass to the copied object.

    This means whenever inject_request is called, it will use the old app’s process_request to run with, which will use the environment of the previous instance.

    Of course, if the old app has any side effects upon process_request, these side effects will happen when the testing application runs as well, as the old app is completely copied over.

  • base_context (Optional[Context]) – The base context to use for this.

Context handler that allows with TestKyoukai.testing_bp() as bp:

You can then register items onto this new root blueprint until __exit__, which will then destroy the blueprint.

Return type:_TestingBpCtxManager
coroutine inject_request(self, headers, url, method='GET', body=None)[source]

Injects a request into the test client.

This will automatically create the correct context.

  • headers (dict) – The headers to use.
  • body (Optional[str]) – The body to use.
  • url (str) – The URL to use.
  • method (str) – The method to use.
Return type:



The result.


Finalizes the app and blueprints.

This will calculate the current werkzeug.routing.Map which is required for routing to work.

Parameters:map_options – The options to pass to the Map for routing.
Return type:Map
coroutine handle_httpexception(self, ctx, exception, environ=None)

Handle a HTTP Exception.

Return type:



A werkzeug.wrappers.Response that handles this response.

log_route(request, code)

Logs a route invocation.

  • request (Request) – The request produced.
  • code (int) – The response code of the route.
coroutine process_request(self, request, parent_context)

Processes a Request and returns a Response object.

This is the main processing method of Kyoukai, and is meant to be used by one of the HTTP server backends, and not by client code.

  • request (Request) – The werkzeug.wrappers.Request object to process. A new HTTPRequestContext will be provided to wrap this request inside of to client code.
  • parent_context (Context) – The asphalt.core.Context that is the parent context for this particular app. It will be used as the parent for the HTTPRequestContext.
Return type:



A werkzeug.wrappers.Response object that can be written to the client as a response.


Registers a child blueprint to this app’s root Blueprint.

This will set up the Blueprint tree, as well as setting up the routing table when finalized.

Parameters:child (Blueprint) – The child Blueprint to add. This must be an instance of Blueprint.

alias of Request


alias of Response

Return type:Blueprint
Returns:The root Blueprint for the routing tree.
run(ip='', port=4444, *, component=None)

Runs the Kyoukai server from within your code.

This is not normally invoked - instead Asphalt should invoke the Kyoukai component. However, this is here for convenience.

coroutine start(self, ip='', port=4444, *, component=None, base_context=None)

Runs the Kyoukai component asynchronously.

This will bypass Asphalt’s default runner, and allow you to run your app easily inside something else, for example.

  • ip (str) – The IP of the built-in server.
  • port (int) – The port of the built-in server.
  • component – The component to start the app with. This should be an instance of KyoukaiComponent.
  • base_context (Optional[Context]) – The base context that the HTTPRequestContext should be started with.