Commit ebdec501 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Modify the command run. It has two subcommands: pytest and script.

parent 7771205e
# -*- coding: utf-8 -*-
""" NAME
pytest -- run the p.ytest framework
_run_pytest -- private script
SYNOPSIS
run the python tests in the context of the web2py application
_run_pytest cmd
DESCRIPTION
Private script launch by the ./run pytest
python tests are developed in the pytest framework.
Dedicated modules are located in the directory myapplication/tests.
The script run these tests in myapplication environment giving
access to the database, the models, ...
OPTIONS
access to the database, the models and the modules...
EXAMPLE
> cd ...track_publications/scripts
> ./run pytest.py
The argument cmd contains the options of the py.test script.
The cmd string is build by the run script when it is used with
the pytest subcommand
AUTHOR
R. Le Gac -- Sep 2015
......@@ -28,5 +25,10 @@
if __name__ == "__main__":
import pytest
from argparse import ArgumentParser
PARSER = ArgumentParser()
PARSER.add_argument("cmd")
ARGS = PARSER.parse_args()
pytest.main("-x -s applications/track_publications/tests")
pytest.main(ARGS.cmd)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" NAME
track_publications -- run a script within track_publications
run -- run a script or test within track_publications
SYNOPSIS
track_publications [options] script.py
run [options] <command> [args]
DESCRIPTION
run the script in the web2py context when the track_publications
model is instantiated.
The run command has to be run in the scripts directory
It can execute two commands script and pytest.
The first command, script, run a given script in the application
context (model, modules and database are available).
The second command, pytest, run pythons test within the pytest
framework as well as the application context.
The python tests are located in the directory myapplication/tests.
The path of the test are relative to that directory.
OPTIONS
-h, --help
Display the help and exit.
-S, --shell
By default the application is track_publications.
This allow to select another one.
EXAMPLE
> cd ... track_publications/scripts
> ./track_publications myscript.py
> ./run -h
> ./run script -h
> ./run script fix-report-number.py
> ./run pytest -h
> ./run pytest
> ./run pytest -xq test_foo/test_faa.py
> ...
AUTHOR
R. Le Gac
"""
if __name__ == "__main__":
import os
import pytest
import subprocess
import sys
import os
import subprocess
import sys
PYTEST_SCRIPT = '_run_pytest.py'
TEST_DIR = 'tests'
WEB2PY_DIR = '../../web2py'
WEB2PY = os.path.join(WEB2PY_DIR, 'web2py.py')
from argparse import ArgumentParser
APP = 'track_publications'
WEB2PY_DIR = '../../web2py'
WEB2PY = os.path.join(WEB2PY_DIR, 'web2py.py')
def get_application():
"""Return the name of the current application.
The following file structure is assumed:
# command line options
parser = ArgumentParser()
parser.add_argument('-S', '--shell',
default=APP,
help="run web2py in interactive shell "
"with specified appname [%(default)s]")
myapplication/scripts
Returns:
string: the name of the application
"""
return os.getcwd().split(os.sep)[-2]
parser.add_argument('script',
help='the name of the script to be run.')
parser.add_argument('args',
nargs='*',
help='list of arguments to be passed to the script.')
def run_pytest(args):
"""Run python test.
args = parser.parse_args()
"""
cmd = ""
# run the script in the track_publication context
script_path = os.path.join(os.getcwd(), args.script)
# collect active option
for k, v in vars(args).iteritems():
if not v or k in ("func", "path", "shell"):
continue
elif k == "capture":
cmd = "%s --%s %s" % (cmd, k, v)
else:
cmd = "%s --%s" % (cmd, k)
# add the path for a given test
path = os.path.join("applications", args.shell, TEST_DIR)
if args.path:
path = os.path.join(path, args.path)
cmd = "%s %s" % (cmd, path)
# run pytest script in the application context
script_path = os.path.join(os.getcwd(), PYTEST_SCRIPT)
cmd = [WEB2PY,
'--no-banner',
'--shell', args.shell,
'--import_models',
'--run', script_path]
'--run', script_path,
'--args', cmd]
return_code = subprocess.call(cmd)
sys.exit(return_code)
def run_script(args):
"""Run the given script.
"""
# run the script in the application context
script_path = os.path.join(os.getcwd(), args.filename)
cmd = [WEB2PY,
'--no-banner',
'--shell', args.shell,
'--import_models',
'--run', script_path]
if args.args:
cmd.append('--args')
cmd.append('--ARGS')
cmd.extend(args.args)
return_code = subprocess.call(cmd)
# exit
sys.exit(return_code)
if __name__ == "__main__":
from argparse import ArgumentParser
# protection
if os.getcwd().split(os.sep)[-1] != "scripts":
print "Should be run in the scripts directory."
sys.exit(1)
if not os.path.exists(WEB2PY):
print "The application web2py is not located in ../../web2py."
sys.exit(1)
# command line options
PARSER = ArgumentParser()
PARSER.add_argument("-S", "--shell",
default=get_application(),
help="run web2py in interactive shell "
"with specified appname [%(default)s]")
SUBPARSERS = PARSER.add_subparsers(title="subcommands",
description="valid subcommands",
help="additional help")
# parser for the pytest subcommand
PYTEST = SUBPARSERS.add_parser("pytest",
help="run python unitary tests.")
PYTEST.add_argument("-q", "--quiet",
action="store_true",
help="minimal output.")
PYTEST.add_argument("--capture",
choices=["fd", "sys", "no"],
help="per-test capturing method: one of fd|sys|no.")
PYTEST.add_argument("-x" , "--exitfirst",
action="store_true",
help='stop after first failure.')
PYTEST.add_argument("-v", "--verbose",
action="store_true",
help="verbose mode.")
PYTEST.add_argument("path",
nargs="?",
help="test path relative to the test directory.")
PYTEST.set_defaults(func=run_pytest)
# parser for the script subcommand
SCRIPT = SUBPARSERS.add_parser("script",
help="run dedicated python script.")
SCRIPT.add_argument("filename",
help="the name of the python script.")
SCRIPT.add_argument("args",
nargs="*",
help="additional arguments to run the script.")
SCRIPT.set_defaults(func=run_script)
# parse the command line and run the selected command
ARGS = PARSER.parse_args()
ARGS.func(ARGS)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment