pchomik / pytest-spec

Library pytest-spec is a pytest plugin to display test execution output like a SPECIFICATION.
GNU General Public License v2.0
100 stars 19 forks source link

Add option to use docstring summary as test name #19

Closed Hubro closed 3 years ago

Hubro commented 7 years ago

Take this test:

def test_hash_password():
    """hash_password should produce a hashed password

    Only the first line should be used as a test name.
    """

    assert True

This currently produces:

test/auth_test.py::
    [PASS]  Hash password

I would like it to produce:

test/auth_test.py::
    [PASS]  hash_password should produce a hashed password

Would it be possible to add that as an option?

Or even better, add an option called (for example) spec_use_docstring_summary that, if set to true, makes pytest-spec read docstrings like these:

def test_hash_password():
    """[hash_password] should produce a hashed password

    Only the first line should be used as a test name.
    """

    assert True

And produce reports like these:

test/auth_test.py::
    hash_password
        [PASS]  should return bytes
        [PASS]  should produce a hashed password
        [PASS]  should produce a different hash when changing salt

    compare_passwords
        [PASS]  should return True when passwords are identical
        [PASS]  should return False when passwords are different
        [PASS]  should be constant-time

    [PASS]  This test didn't follow the above docstring convention
    [PASS]  Not this test either
Hubro commented 7 years ago

The first half of my request could be implemented by reading the docstring and making the first line available as a format string variable:

[pytest]
spec_test_format = [{result}]  {docstring_summary}
pchomik commented 3 years ago

Hi,

I looked into code and it is possible to add docstring_summary variable without huge effort. After implementation 3 variables will be available for spec_test_format:

Regarding format I would like stay with current approach. The logic is generic and is working correctly with multiple solutions.

pchomik commented 3 years ago

Will be part of next release.