importing modules to test file using pytest

1,691 views
Skip to first unread message

monotasker

unread,
Dec 5, 2012, 3:12:58 PM12/5/12
to
[edit: sorry, I forgot the runtest.py attachment before.]
I'm trying to use pytest (instead of unittest) to do unit testing for a web2py app. I've written a script (attached: runtest.py) to launch py.test, which then finds and executes my test files. I run this launcher script in a web2py environment like this:

    python ~/web/web2py/web2py.py -S paideia -M -R applications/paideia/bin/runtest.py

The problem is that when I try to import gluon or any of my app's custom modules (where the classes under test live) I get an import error.

This is particularly strange since my runtest.py explicitly adds the app modules folder to sys.path (this is mostly cribbed from the testrunner.py on web2py slices). But my grasp of the w2p environment and running subprocesses is weak at best. (Case in point, testrunner.py passes globals() to the test files using execfile(testfile, globals()) but I haven't figured out yet how to get pytest to pick up those globals, since it doesn't allow me to execute the test files directly.)

Any help is much appreciated. If we can get this working I think it would be a help, since some of pytest's functions (e.g., parameterized fixtures and automatic fixture clean-up) are pretty powerful.

runtest.py

Vinicius Assef

unread,
Dec 5, 2012, 4:02:51 PM12/5/12
to web...@googlegroups.com
Hi monotasker.

I'm starting to run something like that and I'm giving
gluon/contrib/webclient.py a try.

I intend to prepare an environment to allow running tests using both
unittest2 or pytest.

I think next week I'll already have a decently running environment.

--
Vinicius Assef
> --
>
>
>

monotasker

unread,
Dec 5, 2012, 4:14:32 PM12/5/12
to web...@googlegroups.com
That would be great. I'd really appreciate seeing the code whenever you have a working version. I'm also interested in figuring out why the module imports aren't working in my existing script, since there's obviously something I'm missing about running files in a web2py environment.

Best,

Ian

Vinicius Assef

unread,
Dec 5, 2012, 4:30:23 PM12/5/12
to web...@googlegroups.com
Sure you'll see it.

I'll put it in a github repo.
> --
>
>
>

Ian W. Scott

unread,
Dec 17, 2012, 11:41:13 AM12/17/12
to web...@googlegroups.com
After a bit of hacking I put together a working test runner for pytest. Here it is:

"""
Run this app's tests via py.test

place this file in applications/<appname>/bin/

run with: python <your web2py dir>/web2py.py -S paideia -M -R applications/<appname>/bin/runtest.py
"""

import os
import sys
import pytest

def run_pytest(w2p_dir, test_dir, app_name):
   
if os.name == 'nt':
        errlist
= (WindowsError,ValueError,SystemExit)
   
else:
        errlist
= (OSError,ValueError,SystemExit)

   
try:
        test_dir
= os.path.join(w2p_dir, 'applications', app_name, test_dir)
       
if test_dir not in sys.path:
            sys
.path.append(test_dir)   # to support imports from current folder in the testfiles
       
# modules are applications/[app_name]/modules
        modules_path
= os.path.join('applications', app_name, 'modules')
       
if modules_path not in sys.path:
            sys
.path.append(modules_path)       # to support imports from modules folder
       
if 'site-packages' not in sys.path:
            sys
.path.append('site-packages')    # support imports from web2py/site-packages
        pytest
.main([test_dir]) # run pytest programmatically

   
except Exception, e:
       
print type(e), e

if __name__=='__main__':
    run_pytest
(<your web2py directory>, <your test folder>, <your appname>)

I'll post a slice to w2py slices with this code as well and post the file in a github repo. I'm not a brilliant programmer, so I'm sure others might have improvements to suggest (esp. since right now you have to configure things in the test runner file itself). But it's a working start.


On Wednesday, December 5, 2012 3:06:45 PM UTC-5, Ian W. Scott wrote:
[edit: sorry, I forgot the runtest.py attachment before.]

Vinicius Assef

unread,
Dec 17, 2012, 11:54:17 AM12/17/12
to web...@googlegroups.com
Nice, Ian.

I'm overworking these days. I'll touch this next week.
> --
>
>
>

Mandar Vaze

unread,
Mar 6, 2014, 9:50:41 AM3/6/14
to web...@googlegroups.com
Ian

The following is still useful after more than a year. Thanks !!

I made some modifications - mostly my specific paths before calling run_pytest, and my run_test.py is in scripts folder rather than bin.

I'm getting an error :

NameError: name 'db' is not defined

I'm running it as :

python web2py.py -S myapp -M -R applications/myapp/scripts/run_tests.py

Any ideas ?

-Mandar

Mandar Vaze

unread,
Mar 21, 2014, 2:47:33 PM3/21/14
to web...@googlegroups.com
All,

On Monday, December 17, 2012 10:11:13 PM UTC+5:30, Ian W. Scott wrote:
I realized that global "db" is available until we call pytest.main()
As soon as we are inside pytest.main() - global db object is lost.

Is there a way to preserve the globals() ?

pytest.main() is a just a function call - so ideally globals() should remain intact.
Any idea how to ensure that globals like db, Auth are preserved and passed to the py.test runtime ? (to prevent "NameError: global name 'db' is not defined

Thanks,
-Mandar
Reply all
Reply to author
Forward
0 new messages