Package: sawpit
sawpitWork in progress fork/continuation ofpywsitestA python API to assist with automated websocket integration testingInstallation// TODOPackage contentsWSTestWSTest is the main test running class in sawpit. It currently has the following methods:with_parameter: add a query parameter to the connectionwith_header: add a header to the connectionwith_response: add an expected response to the test runnerwith_message: add a message for the test runner to send on connectionwith_request: attach a rest api request to the instance of this classwith_response_timeout: set the timeout in seconds for the test runner to wait for a response from the websocketwith_message_timeout: set the timeout in seconds for the test runner to wait while trying to send a message to the websocketwith_request_timeout: set the timeout in seconds for the rest request attached to the instance of this classwith_test_timeout: set the timeout in seconds for the test runner to run forwith_received_response_logging: enable logging of received responses on response timeout errorrun: asyncronously run the test runner, sending all messages and listening for responsesis_complete: check whether all expected responses have been received and messages have been sentWSResponseWSResponse is a class to represent an expected response from the websocketwith_attribute: add an attribute to check an incoming response againstwith_trigger: add a message to trigger when a response matching this instance has been receivedis_match: check whether a received response matches the attributes of this instanceWSMessageWSMessage is a class to represent a message to send to the websocketwith_attribute: add an attribute to the message to be sent to the websocket hostwith_delay: add a delay to the message to be sent to the websocket hostRestRequestRestRequest is a class to represent a request to send to rest apiwith_header: add a header to the request to be sent to the rest apiwith_body: add a body to the request to be sent to the rest apiwith_delay: add a delay to the request to be sent to the rest apiExamplesResponse testingTesting a response with a body is received on connection to a websocket host:fromsawpitimportWSTest,WSResponsews_test=(WSTest("wss://example.com").with_response(WSResponse().with_attribute("body")))awaitws_test.run()assertws_test.is_complete()Testing that a response with the following more complicated body is received on connection to a websocket host:{"body":{"attribute":"value"}}fromsawpitimportWSTest,WSResponsews_test=(WSTest("wss://example.com").with_response(WSResponse().with_attribute("body").with_attribute("body/attribute","value")))awaitws_test.run()assertws_test.is_complete()Testing that a response with the following body with a list is received on connection to a websocket host:{"body":[{"colour":"red"},{"colour":"green"},{"colour":"blue"}]}fromsawpitimportWSTest,WSResponsews_test=(WSTest("wss://example.com").with_response(WSResponse().with_attribute("body/0/colour","red").with_attribute("body/1/colour","green").with_attribute("body/2/colour","blue")))awaitws_test.run()assertws_test.is_complete()Testing that a response with the following body with a list containing the colourgreensomewhere is received on connection to a websocket host:{"body":[{"colour":"red"},{"colour":"green"},{"colour":"blue"}]}fromsawpitimportWSTest,WSResponsews_test=(WSTest("wss://example.com").with_response(WSResponse().with_attribute("body//colour","green")))awaitws_test.run()assertws_test.is_complete()Message sendingSending a message on connection to a websocket host:fromsawpitimportWSTest,WSMessagews_test=(WSTest("wss://example.com").with_message(WSMessage().with_attribute("body","Hello, world!")))awaitws_test.run()assertws_test.is_complete()Triggering a message to be sent with extracted data when the following response is received:{"body":{"message":"Hello, world!"}}fromsawpitimportWSTest,WSResponse,WSMessagews_test=(WSTest("wss://example.com").with_response(WSResponse().with_attribute("body/message").with_trigger(WSMessage().with_attribute("body","${body/message}"))))awaitws_test.run()assertws_test.is_complete()Triggering a message to be sent with the first colour extracted from a list when the following response is received:{"body":[{"colour":"red"},{"colour":"green"},{"colour":"blue"}]}fromsawpitimportWSTest,WSResponse,WSMessagews_test=(WSTest("wss://example.com").with_response(WSResponse().with_attribute("body/0/colour").with_trigger(WSMessage().with_attribute("body","${body/0/colour}"))))awaitws_test.run()assertws_test.is_complete()Using rest requestsAttaching simple rest get request and sending it:rest_request=(RestRequest("https://example.com","GET").with_body({"some_key":some_value}))ws_test=(WSTest("wss://example.com").with_request(rest_request))awaitws_test.run()forresponseinws_tester.received_request_responses:print(response.status_code)print(response.json())assertws_test.is_complete()Error handlingForce a test to fail is execution takes more than 30 seconds (default 60 seconds)ws_test=(WSTest("wss://example.com").with_test_timeout(30).with_response(WSResponse().with_attribute("body")))awaitws_test.run()assertws_test.is_complete()Force a test to fail if no response is received for 15 seconds (default 10 seconds)Any responses that haven't been sent will be output along with theWSTimeoutErrorReceived responses can be output too by callingwith_received_response_loggingon theWSTestinstancews_test=(WSTest("wss://example.com").with_response_timeout(15).with_received_response_logging().with_response(WSResponse().with_attribute("body")))awaitws_test.run()assertws_test.is_complete()Force a test to fail is a message takes longer than 15 seconds to send (default 10 seconds)The message that the test runner failed to send will be output along with theWSTimeoutErrorws_test=(WSTest("wss://example.com").with_message_timeout(15).with_message(WSMessage().with_attribute("body","Hello, world!")))awaitws_test.run()assertws_test.is_complete()DocumentationUsers can get the docstring help by running:fromsawpitimportWSTesthelp(WSTest.with_response)LinksGithubPyPI // TODOTest PyPI // TODO