Some discussion here https://groups.google.com/forum/#!topic/web2py/4HdkjbnlhBU
There should be a link in web2py docs to suggest the best way to test. Regards 2018-03-07 15:10 GMT+00:00 Kevin Huang <szu...@gmail.com>: > > Hi all, > > > I'm relatively new to web2py, and still trying to find more resources, but > i've hit a wall, and I'm not quite sure on this issue I'm working with: > > > So working with the "guide" from http://web2py.com/AlterEgo/ > default/show/260, I'm trying to create a few unit tests to test some > RESTFUL api functions. However, while testing multiple test suites, it > seems that the request information is carrying over somehow despite my > attempts to make sure that I start with a new request object via setUp each > time. > > The tests will run just fine if they're alone in the suite, but running > some of them after the other I was running into HTTP 400 errors and during > debugging I found out that the request data carrying over. > > Some "sample" code to illustrate what I was trying to do... > > import unittest > from gluon.globals import Request > > # import api code > execfile("applications/appname/controllers/app_api.py", globals())class > > > # Test Put api > TestPutAPI(unittest.TestCase): > > def setUp(self): > request = Request(env=[]) # new request, env must be set. > > def test_some_function(self): > request.vars['var1'] = 1 > request.vars['var2'] = 2 > request.env.request_method = 'PUT' > response = api() # call api > > self.assertEquals(response['result'], 'success') > > def tearDown(self): > db.table.truncate() # clean up db (nothing request related here) > > > # Test Post api > TestPostAPI(unittest.TestCase): > > def setUp(self): > request = Request(env=[]) # new request > > def test_some_function(self): > request.vars['var5'] = "asdf" > request.vars['var6'] = 6 > request.env.request_method = 'POST' > response = api() # call api > > self.assertEquals(response['result'], 'success') > > def tearDown(self): > db.table.truncate() # clean up db (nothing request related here) > > > # Run tests > suite = unittest.TestSuite() > suite.addTest(unittest.makeSuite(TestPutAPI)) > suite.addTest(unittest.makeSuite(TestPostAPI)) > > unittest.TextTestRunner(verbosity=2).run(suite) > > > Is my entire approach to unit testing wrong? I know there's a lot a > mention of doctests being recommended, also that the line request = > Request() recommended by the original article also being wrong (or simply > out of date), but I'm also unsure of how the request and api call is being > processed in this scenario. Earlier I tried doing 2 calls to the same api > in one test function and found that web2py (and this unittest) simply could > not overwrite the request function - if that made any sense (it didn't, and > didn't work). > > Anyone have any pointers? > > Thank you, > KH > > > -- > Resources: > - http://web2py.com > - http://web2py.com/book (Documentation) > - http://github.com/web2py/web2py (Source code) > - https://code.google.com/p/web2py/issues/list (Report Issues) > --- > You received this message because you are subscribed to the Google Groups > "web2py-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to web2py+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.