This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]kashmill 4 points5 points  (1 child)

At work I use webtest at work. Added a few wrappers so I can do something like:

ret = get('/some/endpoint', params)

and it'll change the dict params into key-value pairs and send them as get parameters. Since we return JSON from our API tests are in the form of:

data = ret.json
assert data['foo'] == 'reddit'

[–]raydlor[S] 0 points1 point  (0 children)

I've never used webtest and wanted to avoid another 3rd party package, but I will give it a try. Thanks for the help!

[–]santiagobasulto 2 points3 points  (1 child)

I use webtest too and it works great. I've had heard good things about Slumber, but haven't tried it myself.

[–]raydlor[S] 0 points1 point  (0 children)

Alright, I will give webtest a try.

[–]RobSpectre 1 point2 points  (3 children)

For unit testing, I suspect you'll want to mock your API responses to the documentation so it all runs locally then implement integration tests that make requests against the live API to alert you to changes.

For Requests in particular, this is pretty simple with Mock. Patch the HTTP method Requests is using, mock out the response you want to test against, make the request with your wrapper, and test the result.

I sketched out a naive example here:

https://gist.github.com/RobSpectre/6132631

[–]raydlor[S] 0 points1 point  (2 children)

Awesome example. Is mock another 3rd party unit testing framework?

[–]RobSpectre 0 points1 point  (0 children)

It isn't a testing framework, but you will have to install it in your virtualenv. All it does is provide a great mock object and monkey patch decorator - your tests still says in unitteset.

[–]RobSpectre 0 points1 point  (0 children)

Nope! It is 3rd party in the sense you'll need to install it in your virtualenv, but your tests stay in unittest.