Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

python - How to unit-test a pytest plugin's hook acting on session-specific data?

I have a pytest plugin written to support a hardware-testing process. The plugin has two key features: a session-scoped db fixture that allows tests to write various information to a database, and a pytest_sessionfinish() hook that I want to use to update the database with the final exitstatus value.

The hook of course does not have access to the fixture; at the moment, this is handled by creating an ad hoc Session.db member containing the same object; this is stored by the db fixture instantiation code itself. If there is a better approach to exposing the fixture object to the hook, I'd like to hear about it.

I believe what I have works fine, but I would like to have a unit test for the plugin to verify that the hook calls into the db correctly. I have a testdir-based unit test that executes a passing test, but the callback to the db fixture is never made because the Session used inside of testdir.inline_run() and passed to the hook is a different Session than the one with the db access object. And since pytest_sessionfinish() is called at the very end of the process, I can't test for this condition within the tests executed by inline_run().

So my question is, how can I verify the call into the fixture from the hook?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...