pytest-html icon indicating copy to clipboard operation
pytest-html copied to clipboard

Add expanding the environment variables in csspath

Open Laymik0o opened this issue 2 years ago • 4 comments

Custom CSS (Cascasding Style Sheets) can be passed on the command line using the --css option In the current realization pytest-html checks existing of the css file with the next code

for csspath in config.getoption("css"):
    if not Path(csspath).exists():
        missing_css_files.append(csspath)

Сould you add the ability to use environment variables in the css file path? This has already been implemented for html path in HTMLReport class by os.path.expandvars

It would look like:

for csspath in config.getoption("css"):
    absolute_css_path = Path(os.path.expandvars(csspath)).expanduser().absolute()
    if not absolute_css_path.exists():
        missing_css_files.append(absolute_css_path)

Laymik0o avatar Apr 20 '23 14:04 Laymik0o

I'll look into it.

BeyondEvil avatar Apr 22 '23 07:04 BeyondEvil

Ok, I've started something.

Questions:

  • Why does the path have to be absolute?
  • Are you using "--self-contained-html"?
  • Could you provide a specific use-case? I'll use that to create a test.

ping @Laymik0o

BeyondEvil avatar Apr 22 '23 08:04 BeyondEvil

Hi @BeyondEvil,

  1. The absolute path is not mandatory, just a first idea. You can remove it and it will work the same
  2. Yes, I do
  3. Tests can be divided into suites or features and have a complex folder structure. If pytest is launched for some specific module that placed deep in the structure, the described problem may occur. I first faiced it when I worked through PyCharm and, out of habit, launched the module with tests through Right Click - Run "Python tests in ..."

Example of structure:

/tests
    /feature1
        /smoke_suite
            __init__.py
            i_want_run_these_tests.py
            i_dont_want_to_run_it_now.py
        /outdated_tests_but_still_keep_it
    /feature2
    /feature3
    conftest.py
    pytest.ini
    pytest_style.css

Pytest has the next args --html=%LOCALAPPDATA%/temp/tests_report.html --self-contained-html --css=pytest_style.css

So, if I run directle i_want_run_these_tests.py module then I will get error with "missing css files" because it will try to search css in smoke_suite folder If csspath could include system variables, then I would simply specify the path to the working directory where the file is located

Example: --html=%LOCALAPPDATA%/temp/tests_report.html --self-contained-html --css=%TESTS%/pytest_style.css

Laymik0o avatar Apr 24 '23 12:04 Laymik0o

A small note for those who run into this problem, can be useful As a workaround I add the following code to conftest.py

def pytest_configure(config):
    config.option.css[0] = os.path.join(os.path.dirname(__file__), config.option.css[0])

Laymik0o avatar Apr 24 '23 13:04 Laymik0o