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

-- 



Reply via email to