idk if that is the right way... i'm using this approach in on of my projects and at the moment the only problem i have is with the memory usage or the slow access with a file base sqlite database for the tests.

On Wed, Mar 7, 2018 at 1:08 PM, António Ramos <ramstei...@gmail.com> wrote:
Next chapter in web2py book

Pytest your app the right way

2018-03-07 18:02 GMT+00:00 Yoel Benítez Fonseca <ybenit...@gmail.com>:
H!

checkout: https://github.com/viniciusban/web2py.test



On Wed, Mar 7, 2018 at 10:10 AM, Kevin Huang <szu...@gmail.com> wrote:

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.

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

Reply via email to