Search code examples
bashshellpytestquotation-marks

Quotation marks inside bash script for pytest


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


Solution

  • 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[@]}"