Open b0g3r opened 3 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.
How to test pytest plugins https://docs.pytest.org/en/stable/writing_plugins.html#testing-plugins
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:
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
Sorry, I pushed it into the master branch, but I did it. Can you check it? 8de5070e75f887691ff61b688e15c19033258a8d
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?
snapshottest
, but with interactive mode while updating