pchomik/pytest-spec

Add option to use docstring summary as test name

Hubro opened this issue · 3 comments

Hubro commented

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

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}

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:

  • result
  • name
  • docstring_summary

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

Will be part of next release.