zaufi / pytest-matcher

A pytest plugin to match test output against patterns stored in files
https://pytest-matcher.readthedocs.io
2 stars 1 forks source link

Make the `expected_out` instance printable #1

Open zaufi opened 6 years ago

zaufi commented 6 years ago

... i.e. convertible to string, so it can be printed w/ ordinal print(expected_out)

xymaxim commented 9 months ago

Currently, the plugin fixtures store and read a pattern file content on an assertion line. Thus, it's not possible to access the content before that. The reading should be moved out from the assert (https://docs.pytest.org/en/6.2.x/fixture.html#what-fixtures-are) step to the arrange one. Since the fixtures are called just before a test run, it could look like the following:

@pytest.fixture()
def expected_out(request):
    # If store not requested, read an expectation file's content here:
    ...

def test_printing(expected_out):
    print(expected_out._expected_file_content)
    # By creating a new public attribute,  e.g.  `content` or `output`:
    print(expected_out.content)
    # Or, with adding the __str__() method, just:
    print(expected_out)

With this approach, each check test run will read an expectation file, even without the assert statement.

Do you see any drawbacks here?

zaufi commented 9 months ago

Sorry, I didn't get the point of reading ahead of time when the content became actually needed… producing unnecessary disk I/O (e.g., in a case when a test w/o any print(expected_out) failed on another assert before the one checking expected output), losing laziness, &etc. Why not override __str__, or __repr__, or whatever print() calls on an instance of an arbitrary object and perform reading here (caching the result, of course, for the future reference by assert)?

xymaxim commented 9 months ago

Ah, I got your point about the laziness. It sounds reasonable, and it fits printing as well as editing, thanks.