top of page
  • Writer's pictureTim Burns

Curl to PyTest

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


{"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}{color}/roll": "Usage:", f"https://{api_id}{color}/roll/1d4": '{"count": "1", "dice": "4"', f"https://{api_id}{color}/roll/2d4+2": '{"count": "2", "dice": "4", "modifier": "+2"', f"https://{api_id}{color}/roll/1d20-3": '{"count": "1", "dice": "20", "modifier": "-3"', f"https://{api_id}{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 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\ . [100%]

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


288 views0 comments

Recent Posts

See All

Providence Porchfest '24

Maddie Cardoza at Providence Porchfest '24 Had a wonderful day yesterday at Porchfest.


bottom of page