Skip to content

Instantly share code, notes, and snippets.

@jeevan449
Created December 22, 2017 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeevan449/c02ed468fc456589e9496dcc82d00e6f to your computer and use it in GitHub Desktop.
Save jeevan449/c02ed468fc456589e9496dcc82d00e6f to your computer and use it in GitHub Desktop.
sample restapi example with nose
import requests
import json
from nose.tools import assert_equal
surl = 'https://my-json-server.typicode.com/jeevan449/pythonlearning'
class requestsexample:
def __init__(self,url):
self.surl = url
def getdata(self,path):
self.path=path
self.req=(self.surl+self.path)
r = requests.get(self.req)
data = r.json()
code = r.status_code
return data
def postdata(self,path,params):
self.params=params
self.path=path
self.url=self.surl+self.path
r = requests.post(self.url,data = self.params)
data1 = r.json()
code = r.status_code
return data1
obj = requestsexample(surl)
def test_postdata():
path= '/posts'
values = {'id':4, 'title':'jeevan'}
res = obj.postdata(path,values)
assert_equal(res['title'],'jeevan')
def test_getuserdata():
path = '/posts/1'
res = obj.getdata(path)
assert_equal(res['title'],'Post 1')
def test_postdata_fail():
path= '/posts'
values = {'id':5, 'title':'chaitanya1'}
res = obj.postdata(path,values)
assert_equal(res['title'],'chaitanya')
def test_getuserdata_fail():
path = '/posts/1'
res = obj.getdata(path)
assert_equal(res['title'],'Post 2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment