Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run custom command with tox without specifying it in tox.ini?

I am trying to verify commands before placing them in tox.ini under [testenv] commands = section.

Is it possible to pass custom command to tox by passing it as shell arguments? Something like

tox -e <env_to_run_script_in> <command_which_we_want_to_run_in_specified_env>

I have tried the following but none of them works.

tox -e py34 args py.test
tox -e py34 -- py.test
tox args "py.test"

How can I run python commands/scripts in tox created virtual environments without placing them in tox.ini ?

like image 814
avimehenwal Avatar asked Dec 10 '15 13:12

avimehenwal


People also ask

How does tox INI work?

Tox is a tool that creates virtual environments, and installs the configured dependencies for those environments, for the purpose of testing a Python package (i.e. something that will be shared via PyPi, and so it only works with code that defines a setup.py ).

What is tox Envlist?

[tox]envlist is only a default — a list of environments to run when tox is invoked without option -e and without TOXENV environment variable. Once you use tox -e [tox]envlist is ignored. You can run local environment with different python versions, but I don't know any way to run it multiple times.


1 Answers

By using posargs with a default argument in the command specifier, arbitrary command lines can be passed to the underlying virtualenv environment while still running the tests when no arguments are passed.

Using a tox.ini like

[tox]
envlist = py27,py35,pypy,pypy3

[testenv]
passenv =
    TERM
deps=
    pytest
    ipython
    six
commands={posargs:py.test}

When tox is invoked with no arguments, it defaults to running py.test otherwise args passed on the command line are sent to the specified virtualenv.

Using a sample hello.py in the root of your project

import os
import sys
print(os.__file__)
print(sys.version)
print("Hello from env")

called via tox -e pypy python hello.py

tox -e pypy launches pypy virtualenv with the arguments python hello.py

Output:

/Users/seanjensengrey/temp/.tox/pypy/lib-python/2.7/os.pyc
2.7.10 (5f8302b8bf9f53056e40426f10c72151564e5b19, Jan 20 2016, 04:41:02)
[PyPy 4.0.1 with GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]
Hello from env

I use TERM="xterm-256color" tox -e pypy ipython to invoke an ipython shell with my package installed in the virtualenv.

like image 120
Sean Jensen-Grey Avatar answered Oct 15 '22 04:10

Sean Jensen-Grey