8

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 ?

anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
avimehenwal
  • 1,502
  • 3
  • 21
  • 30

2 Answers2

9

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.

0

I found myself with this same issue. Most frequently this was a problem when trying to debug a unit test that only failed with a particular interpreter or environment.

I suppose I could use a tool like virtualenvwrapper to help with this, but since my .tox directory already had a virtualenv with the right dependencies -- and in fact the exact set of dependencies that I was trying to debug -- it seemed like the best place to work with.

To make this easier I created a little shell script which I use to run an arbitrary command (even a shell!) inside the tox virtualenv. I called it "toxin". The source is in this gist -- it's not particularly complicated, but hopefully someone finds it useful.

NOTE: Please do not copy the code into this answer. That has the effect of re-licensing it as CC-BY-SA.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135