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 =======================================================================================================


272 views0 comments

Recent Posts

See All

Carto, Snowflake, and Data Management

A basic principle of data management: Don't move data unless you have to. Moving data is expensive and error-prone. Data Egress Cost: How To Take Back Control And Reduce Egress Charges Archiving to S

Music Festivals 2024

I am browsing music festivals I'd like to attend in 2024. I work for which manages vacation properties and my search came up with this site from one of our biggest competitors. Vacasa: US

bottom of page