-1

I try to write a script that helps selecting pytest options depending on user input. Unfortunately, the scripts always fail because of the latest parameter, which relates to the marker.

I have suspect the quotation mark in the marker option to be the origin of the problem. Unfortunately, I cannot find any workaround.

Here is as MWE:

Content of test.sh

#!/bin/bash

options=("-v")
options+=("-m \"not dummy\"")

echo "about to launch pytest ${options[@]}"
pytest ${options[@]}

Content of test_dummy.py:

import pytest

@pytest.mark.dummy
def test_dummy():
    assert(True)

Now the output of running test.sh script:

about to launch pytest -v -m "not dummy" =============================================================================================================================== test session starts =============================================================================================================================== platform linux -- Python 3.6.4, pytest-3.3.2, py-1.5.2, pluggy-0.6.0 -- /home/[...]/anaconda3/bin/python cachedir: .cache rootdir: /home/[...]/pytest, inifile: plugins: cov-2.5.1

========================================================================================================================== no tests ran in 0.00 seconds =========================================================================================================================== ERROR: file not found: dummy"

Of course, running the generated command

pytest -v -m "not dummy"

Works perfectly. How to overcome this problem

Thank you in advance

Tobbey
  • 469
  • 1
  • 4
  • 18
  • `pytest "${options[@]}"` is closer, but won't work since you're passing the single argument `-m "not dummy"` instead of two arguments `-m` and `not dummy` – William Pursell Oct 08 '18 at 12:25
  • It does not work indeed, the last paremeter even seems to be ignored – Tobbey Oct 08 '18 at 12:30

2 Answers2

5

Not sure, but I think you problem comes from defining two arguments as a single one. Try to put all arguments separated:

#!/bin/bash

options=("-v")
options+=("-m" "not dummy")

echo "about to launch pytest ${options[@]}"
pytest "${options[@]}"
Poshi
  • 5,332
  • 3
  • 15
  • 32
-2

Actually, a found a way of solving this. Apparently the easiest solution is to "recast" the whole command as a string, and make eval to evaluate it.

I replaced the last line

pytest ${options[@]}

by

eval pytest ${options[@]}

Tobbey
  • 469
  • 1
  • 4
  • 18
  • See [Why should eval be avoided in Bash, and what should I use instead?](https://stackoverflow.com/q/17529220/4154375). The answer by Poshi contains a good fix for the code. – pjh Oct 08 '18 at 18:48