b0g3r / perseus

WIP: snapshot management tool
MIT License
1 stars 1 forks source link

Roadmap: Version 1.0 #6

Open b0g3r opened 4 years ago

b0g3r commented 4 years ago
Bobronium commented 4 years ago

For test writing we could use semething similar to pydantic.tests.conftest.create_module paired with pytest invoking, like so:

def test_perseus_ok(create_test):
    @create_test  # will create test_module.py and return some special object
    def test_module():
        def test_foo(snapshot):
            assert snapshot.match('foo')
     result = pytest.main([test_module.path])  # or test_module.run()
     ...

I'll try to dig into this on the next weekend.

b0g3r commented 4 years ago

Also check https://github.com/syrusakbary/snapshottest/blob/master/tests/test_pytest.py

b0g3r commented 4 years ago

How to test pytest plugins https://docs.pytest.org/en/stable/writing_plugins.html#testing-plugins

b0g3r commented 4 years ago

PoC with pytester looks like:

from _pytest.pytester import Testdir

def test_1(testdir: Testdir):
    testdir.makepyfile(
        '''
        def test_1(snapshot):
            snapshot.assert_match('test sstring')
        '''
    )
    snapshot_file = testdir.mkpydir('snapshots').ensure('snap_test_1.py')
    snapshot_file.write_text(
        '''
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals

from perseus._vendor.snapshottest import Snapshot

snapshots = Snapshot()

snapshots['test_1 1'] = 'test string'
        ''',
        encoding='utf-8'
    )
    result = testdir.runpytest()
    assert result

The next steps:

b0g3r commented 4 years ago

Yep, it's working. Unfortunately runpytest() doesn't pass stdin kwargs to popen, so I manually copied the method and add stdin 🤦 :

    p = make_numbered_dir(root=Path(str(testdir.tmpdir)), prefix="runpytest-")
    args = ("--basetemp=%s" % p,)
    plugins = [x for x in testdir.plugins if isinstance(x, str)]
    if plugins:
        args = ("-p", plugins[0]) + args
    args = testdir._getpytestargs() + args
    result = testdir.run(*args, stdin=b'y')

It's working 🎉

I'll try to write a convenient wrapper around it with (test, snapshot) -> (pytest_runner, snapshot_file), maybe with copy_example

b0g3r commented 4 years ago

Sorry, I pushed it into the master branch, but I did it. Can you check it? 8de5070e75f887691ff61b688e15c19033258a8d

Bobronium commented 4 years ago

Running subprocess for each test might be really slow. Maybe there is a way to stick to "inprocess" method?

I almost got it to work (with testdir), but had troubles with input suppressing. I'd preferred faster method, if that's doable.

What do you think?

Upd. BTW, maybe we should create separate issue for this discussion?