Best Python code snippet using Airtest
test_challenges.py
Source:test_challenges.py
1import pytest2from starlette.status import HTTP_200_OK, HTTP_403_FORBIDDEN3from explorebaduk.messages import (4 ChallengeAcceptedMessage,5 ChallengeOpenMessage,6 ChallengeRemovedMessage,7 DirectChallengeMessage,8)9from tests.helpers import random_websocket, receive_websockets10@pytest.fixture11def challenge_json() -> dict:12 return {13 "game": {14 "name": "string",15 "private": False,16 "ranked": True,17 "board_size": 19,18 "speed": "blitz",19 "rules": "japanese",20 "time_control": {21 "time_system": "byo-yomi",22 "main_time": 1800,23 "overtime": 30,24 "periods": 5,25 },26 "handicap": None,27 "komi": None,28 },29 "creator_color": "nigiri",30 "min_rating": None,31 "max_rating": None,32 }33@pytest.mark.asyncio34async def test_create_challenge(db, test_cli, websockets, challenge_json):35 creator_ws = random_websocket(websockets)36 test_cli.authorize(creator_ws.user)37 resp = await test_cli.create_challenge(challenge_json)38 assert resp.status_code == HTTP_200_OK, resp.text39 challenge = db.get_challenge_by_id(resp.json()["challenge_id"])40 expected = ChallengeOpenMessage(challenge).json()41 for messages in await receive_websockets(websockets):42 assert expected in messages43@pytest.mark.asyncio44async def test_accept_challenge(db, test_cli, websockets, challenge_json):45 creator_ws = random_websocket(websockets)46 test_cli.authorize(creator_ws.user)47 resp = await test_cli.create_challenge(challenge_json)48 assert resp.status_code == HTTP_200_OK, resp.text49 await receive_websockets(websockets)50 challenge_id = resp.json()["challenge_id"]51 challenge = db.get_challenge_by_id(challenge_id)52 expected = ChallengeAcceptedMessage(challenge).json()53 opponent_ws = random_websocket(websockets, exclude_users=[creator_ws.user])54 test_cli.authorize(opponent_ws.user)55 resp = await test_cli.accept_challenge(challenge_id)56 assert resp.status_code == HTTP_200_OK, resp.text57 for messages in await receive_websockets(websockets):58 assert expected in messages59@pytest.mark.asyncio60async def test_cancel_challenge(db, test_cli, websockets, challenge_json):61 creator_ws = random_websocket(websockets)62 test_cli.authorize(creator_ws.user)63 resp = await test_cli.create_challenge(challenge_json)64 assert resp.status_code == HTTP_200_OK, resp.text65 await receive_websockets(websockets)66 challenge_id = resp.json()["challenge_id"]67 challenge = db.get_challenge_by_id(challenge_id)68 expected = ChallengeRemovedMessage(challenge).json()69 resp = await test_cli.cancel_challenge(challenge_id)70 assert resp.status_code == HTTP_200_OK, resp.text71 for messages in await receive_websockets(websockets):72 assert expected in messages73@pytest.mark.asyncio74async def test_accept_challenge_self(test_cli, websockets, challenge_json):75 creator_ws = random_websocket(websockets)76 test_cli.authorize(creator_ws.user)77 resp = await test_cli.create_challenge(challenge_json)78 assert resp.status_code == HTTP_200_OK, resp.text79 await receive_websockets(websockets)80 challenge_id = resp.json()["challenge_id"]81 resp = await test_cli.accept_challenge(challenge_id)82 assert resp.status_code == HTTP_403_FORBIDDEN, resp.text83@pytest.mark.asyncio84async def test_cancel_challenge_not_self(test_cli, websockets, challenge_json):85 creator_ws = random_websocket(websockets)86 test_cli.authorize(creator_ws.user)87 resp = await test_cli.create_challenge(challenge_json)88 assert resp.status_code == HTTP_200_OK, resp.text89 await receive_websockets(websockets)90 challenge_id = resp.json()["challenge_id"]91 user_ws = random_websocket(websockets, exclude_users=[creator_ws.user])92 test_cli.authorize(user_ws.user)93 resp = await test_cli.cancel_challenge(challenge_id)94 assert resp.status_code == HTTP_403_FORBIDDEN, resp.text95@pytest.mark.asyncio96async def test_create_direct_challenge(db, test_cli, websockets, challenge_json):97 creator_ws = random_websocket(websockets)98 test_cli.authorize(creator_ws.user)99 opponent_ws = random_websocket(websockets)100 challenge_json["opponent_id"] = opponent_ws.user.user_id101 resp = await test_cli.create_challenge(challenge_json)102 assert resp.status_code == HTTP_200_OK, resp.text103 challenge = db.get_challenge_by_id(resp.json()["challenge_id"])104 expected = DirectChallengeMessage(challenge).json()105 assert expected in await opponent_ws.receive()106 for messages in await receive_websockets(websockets):107 assert not messages108@pytest.mark.asyncio109async def test_accept_direct_challenge(db, test_cli, websockets, challenge_json):110 creator_ws = random_websocket(websockets)111 test_cli.authorize(creator_ws.user)112 opponent_ws = random_websocket(websockets, exclude_users=[creator_ws.user])113 challenge_json["opponent_id"] = opponent_ws.user.user_id114 resp = await test_cli.create_challenge(challenge_json)115 assert resp.status_code == HTTP_200_OK, resp.text116 await receive_websockets(websockets)117 challenge_id = resp.json()["challenge_id"]118 challenge = db.get_challenge_by_id(challenge_id)119 expected = ChallengeAcceptedMessage(challenge).json()120 test_cli.authorize(opponent_ws.user)121 resp = await test_cli.accept_challenge(challenge_id)122 assert resp.status_code == HTTP_200_OK, resp.text123 assert expected in await creator_ws.receive()124 for messages in await receive_websockets(websockets):...
test_endpoints.py
Source:test_endpoints.py
1from sanic.testing import SanicTestClient2from ujson import dumps3async def test_index_404(test_cli: SanicTestClient):4 resp_json = {"message": "path does not exist"}5 uri = '/'6 get = await test_cli.get(uri)7 assert get.status == 4048 assert await get.json() == resp_json9 post = await test_cli.post(uri)10 assert post.status == 40411 assert await post.json() == resp_json12 put = await test_cli.put(uri)13 assert put.status == 40414 assert await put.json() == resp_json15 delete = await test_cli.delete(uri)16 assert delete.status == 40417 assert await delete.json() == resp_json18 head = await test_cli.head(uri)19 assert head.status == 40420 options = await test_cli.options(uri)21 assert options.status == 40422 assert await options.json() == resp_json23async def test_v1_404(test_cli: SanicTestClient):24 resp_json = {"message": "path does not exist"}25 uri = '/v1/'26 get = await test_cli.get(uri)27 assert get.status == 40428 assert await get.json() == resp_json29 post = await test_cli.post(uri)30 assert post.status == 40431 assert await post.json() == resp_json32 put = await test_cli.put(uri)33 assert put.status == 40434 assert await put.json() == resp_json35 delete = await test_cli.delete(uri)36 assert delete.status == 40437 assert await delete.json() == resp_json38 head = await test_cli.head(uri)39 assert head.status == 40440 options = await test_cli.options(uri)41 assert options.status == 40442 assert await options.json() == resp_json43async def test_post_names_202(test_cli: SanicTestClient):44 json_req = {'name': 'ÐеÑÑовиÑ'}45 uri = '/v1/names/'46 post = await test_cli.post(uri, data=dumps(json_req))47 assert post.status == 20248 assert 'name' in await post.json()49 assert json_req['name'] in (await post.json()).values()50async def test_post_names_400(test_cli: SanicTestClient):51 json_resp = {52 'message': "key 'name' in body json is not found. please repeat "53 "the request with the key 'name'"54 }55 uri = '/v1/names/'56 post = await test_cli.post(uri)57 assert post.status == 40058 assert json_resp == await post.json()59async def test_get_names_not_found_404(test_cli: SanicTestClient):60 uri = '/v1/names/3556dda5-b1d3-46ed-bb46-7e2d1d312fa3'61 resp = {"message": "Data is not found. Please check 'names'"}62 get = await test_cli.get(uri)63 assert get.status == 40464 assert resp == await get.json()65async def test_get_names_invalid_uuid_400(test_cli: SanicTestClient):66 uri = '/v1/names/3556dda5'67 resp = {"message": "Invalid uuid"}68 get = await test_cli.get(uri)69 assert get.status == 400...
test_cvm.py
Source:test_cvm.py
1from utils import TestCli2def test_describe_regions():3 cmd = 'tccli cvm DescribeRegions'4 expect = "\"Region\": \"ap-guangzhou\""5 test_cli = TestCli()6 test_cli.equal(cmd, expect)7def test_describe_instances():8 cmd = 'tccli cvm DescribeInstances'9 expect = "\"TotalCount\":"10 test_cli = TestCli()11 test_cli.equal(cmd, expect)12def test_describe_disaster_disaster_recover_group_quota():13 cmd = 'tccli cvm DescribeDisasterRecoverGroupQuota'14 expect = "\"CvmInHostGroupQuota\": 50,"15 test_cli = TestCli()16 test_cli.equal(cmd, expect)17def test_describe_disaster_recover_groups():18 cmd = 'tccli cvm DescribeDisasterRecoverGroups'19 expect = "DisasterRecoverGroupSet"20 test_cli = TestCli()21 test_cli.equal(cmd, expect)22def test_describe_hosts():23 cmd = 'tccli cvm DescribeHosts --cli-unfold-argument'24 cmd += ' --Filters.0.Name zone'25 cmd += ' --Filters.0.Values ap-guangzhou-2'26 cmd += ' --Offset 0 --Limit 20'27 expect = "\"HostSet\": []"28 test_cli = TestCli()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!