pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
12.09k stars 2.68k forks source link

pytester's plugin makepyfile function distorting indentation (python 2.7) #4268

Closed Sup3rGeo closed 5 years ago

Sup3rGeo commented 6 years ago

Example test:

def test_compatibility(testdir):
    testdir.makepyfile(test_executor_tests="""
        import pytest
        from typhoon.utils.test_executor_compat import run_test

        var1 = "test level 1"
        var2 = "test level 2"
        var3 = "test level 3"

        test_list = [
            {}
        ]

        @pytest.mark.parametrize("test_spec", [
            pytest.param(item, id=item[0]) for item in test_list
        ])        
        def test_executor_tests(test_spec, request, capsys):
            run_test(test_spec, request, capsys)
    """.format("\n".join(["[asdas]", "[dasdasd]", "[123]"])))

    print(str(testdir.tmpdir))

Now we check the created py file in tmpdir.

pytest-3.8.2:

import pytest
from typhoon.utils.test_executor_compat import run_test

var1 = "test level 1"
var2 = "test level 2"
var3 = "test level 3"

test_list = [
    [asdas]
[dasdasd]
[123]
]

@pytest.mark.parametrize("test_spec", [
    pytest.param(item, id=item[0]) for item in test_list
])        
def test_executor_tests(test_spec, request, capsys):
    run_test(test_spec, request, capsys)

pytest-3.9.1 (onwards):

import pytest
        from typhoon.utils.test_executor_compat import run_test

        var1 = "test level 1"
        var2 = "test level 2"
        var3 = "test level 3"

        test_list = [
            [asdas]
[dasdasd]
[123]
        ]

        @pytest.mark.parametrize("test_spec", [
            pytest.param(item, id=item[0]) for item in test_list
        ])        
        def test_executor_tests(test_spec, request, capsys):
            run_test(test_spec, request, capsys)

This of course fails when running because indentation is screwed up.

Not using virtual environments at all.

PS C:\Users\Victor\Desktop\py27makepy> py -2 -m pip list
Package                       Version
----------------------------- -----------
alabaster                     0.7.11
allure-pytest                 2.4.1
allure-python-commons         2.4.1
atomicwrites                  1.1.5
attrs                         18.1.0
Babel                         2.6.0
backports.functools-lru-cache 1.5
certifi                       2018.4.16
chardet                       3.0.4
colorama                      0.3.9
cycler                        0.10.0
Cython                        0.28.3
docutils                      0.14
enum34                        1.1.6
funcsigs                      1.0.2
functools32                   3.2.3.post2
future                        0.16.0
idna                          2.7
imagesize                     1.0.0
Jinja2                        2.10
kiwisolver                    1.0.1
MarkupSafe                    1.0
matplotlib                    2.2.2
more-itertools                4.2.0
numpy                         1.14.1+mkl
packaging                     17.1
pandas                        0.23.1
pathlib2                      2.3.2
pdfrw                         0.4
PeakUtils                     1.1.1
Pillow                        5.1.0
pip                           10.0.1
pluggy                        0.8.0
py                            1.5.3
pycryptodome                  3.6.4
pycryptodomex                 3.6.6
Pygments                      2.2.0
PyHamcrest                    1.9.0
pyModbusTCP                   0.1.5
pyparsing                     2.2.0
pytest                        3.9.1
python-dateutil               2.7.0
pytz                          2018.3
pyzmq                         17.0.0
reportlab                     3.4.0
requests                      2.19.1
scandir                       1.9.0
scipy                         1.0.0
setuptools                    39.0.1
six                           1.11.0
slycot                        0.2.0
snowballstemmer               1.2.1
Sphinx                        1.7.5
sphinxcontrib-websupport      1.1.0
tox                           3.0.0
Typhoon-HIL-API               1.1.4
TyphoonTest                   1.1.1
typing                        3.6.4
urllib3                       1.23
virtualenv                    15.2.0
virtualenvwrapper-win         1.2.5
wheel                         0.31.1
win-inet-pton                 1.0.1
wrapt                         1.10.11
xlrd                          1.1.0
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
RonnyPfannschmidt commented 6 years ago

your code was fundamentally broken from the beginning, our recent switch from a hackish mess to just deindent just made it more apparent (as the text is now exactly rendered as your code intends)

Sup3rGeo commented 6 years ago

@RonnyPfannschmidt, is it really the case to have the first (import pytest) and second (https://github.com/pyinstaller/pyinstaller.git) line differently indented, even though they have the same indentation in the string? Could you explain why?

nicoddemus commented 6 years ago

@Sup3rGeo it is not obvious, after some debugging I noticed that your text (before being passed to makepyfile) expands to:

        import pytest
        from typhoon.utils.test_executor_compat import run_test
        var1 = "test level 1"
        var2 = "test level 2"
        var3 = "test level 3"
        test_list = [
            [asdas]
[dasdasd]
[123]
        ]
        @pytest.mark.parametrize("test_spec", [
            pytest.param(item, id=item[0]) for item in test_list
        ])        
        def test_executor_tests(test_spec, request, capsys):
            run_test(test_spec, request, capsys)

The indent of lines [dasdasd] and [123] is what's messing with textwrap.dedent, because it is generic and not Python-code specific.

Sup3rGeo commented 5 years ago

Solved this problem of defining indented multiline triple-quote strings using inspect.cleandoc.