Search
  • Tim Burns

Curl to PyTest

Curl is fine for testing manually, but my eyes get tired watching all that JSON mumbo-jumbo.


curl -H "X-API-KEY: ${X_API_KEY}" -X POST https://${API_ID}.execute-api.us-east-1.amazonaws.com/${COLOR}/roll/1d4


Result:

{"count": "1", "dice": "4", "modifier": "", "rolls": [3], "total": 3}


I know this is right because I specified it, but I don't want to think about dice when I'm on to the next thing.


PyTest to the rescue.


def test_configure_json(self): api_id = os.getenv("API_ID") color = os.getenv("COLOR") tests = { f"https://{api_id}.execute-api.us-east-1.amazonaws.com/{color}/roll": "Usage:", f"https://{api_id}.execute-api.us-east-1.amazonaws.com/{color}/roll/1d4": '{"count": "1", "dice": "4"', f"https://{api_id}.execute-api.us-east-1.amazonaws.com/{color}/roll/2d4+2": '{"count": "2", "dice": "4", "modifier": "+2"', f"https://{api_id}.execute-api.us-east-1.amazonaws.com/{color}/roll/1d20-3": '{"count": "1", "dice": "20", "modifier": "-3"', f"https://{api_id}.execute-api.us-east-1.amazonaws.com/{color}/roll/1dd20-3": 'Invalid dice: 1dd20-3', } for test in tests.keys(): print(test) result = requests.get(test) self.assertEqual(200, result.status_code) print(result.text) self.assertTrue(tests[test] in result.text)


There we go. A simple way to run a bunch of CURL tests and match the strings I want to see.


py.test tests/test_curl.py

========================================================================================================= test session starts =========================================================================================================

platform win32 -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0

rootdir: C:\Users\TimB\owlmtn\reporting

collected 1 item


tests\test_curl.py . [100%]


====================================================================================================== 1 passed in 1.16 seconds =======================================================================================================

m


159 views0 comments

Recent Posts

See All

Here is the ticket master public API. https://developer.ticketmaster.com/ Could be a very interesting edition to cross-reference with Playlist info to get hot concerts that may be under the radar.

Elon Musk embodies the worst aspects of the tech bro spirit. He has no moral compass. He lacks compassion. He elevates the vilest voices in our world. He is not the kind of person I would want to