Package: pytest-anki
pytest-ankipytest-anki is apytestplugin that allows developers to write tests for theirAnki add-ons.At its core lies theanki_sessionfixture that provides add-on authors with the ability to create and control headless Anki sessions to test their add-ons in:frompytest_ankiimportAnkiSessiondeftest_addon_registers_deck(anki_session:AnkiSession):my_addon=anki_session.load_addon("my_addon")withanki_session.load_profile()withanki_session.deck_installed(deck_path)asdeck_id:assertdeck_idinmy_addon.deck_idsanki_sessioncomes with a comprehensive API that allows developers to programmatically manipulate Anki, set up and reproduce specific configurations, simulate user interactions, and much more.The goal is to provide add-on authors with a one-stop-shop for their functional testing needs, while also enabling them to QA their add-ons against a battery of different Anki versions, catching incompatibilities as they arise.DisclaimerProject StateImportant: The plugin is currently undergoing a major rewrite and expansion of its feature-set, so the documentation below is very sparse at the moment. I am working on bringing the docs up to speed, but until then, please feel free to check out the inline documentation and also take a look at the plug-in's tests for a number of hopefully helpful examples.Platform Supportpytest-ankihas only been confirmed to work on Linux so far.InstallationRequirementspytest-ankirequires Python 3.8+.Installing the latest packaged build$pipinstallpytest-ankior$poetryadd--devpytest-ankiUsageBasic UseIn your tests add:frompytest_ankiimportAnkiSession# for type checking and completions@pytest.mark.forkeddeftest_my_addon(anki_session:AnkiSession):# add some tests in hereTheanki_sessionfixture yields anAnkiSessionobject that gives you access to the following attributes, among others:app {AnkiApp} -- Anki QApplication instance
mw {AnkiQt} -- Anki QMainWindow instance
user {str} -- User profile name (e.g. "User 1")
base {str} -- Path to Anki base directoryAdditionally, the fixture provides a number of helpful methods and context managers, e.g. for initializing an Anki profile:@pytest.mark.forkeddeftest_my_addon(anki_session:AnkiSession):withanki_session.profile_loaded():assertanki_session.collectionConfiguring the Anki SessionYou can customize the Anki session context by passing arguments to theanki_sessionfixture using pytest's indirect parametrization, e.g.importpytest@pytest.mark.forked@pytest.mark.parametrize("anki_session",[dict(load_profile=True)],indirect=True)deftest_my_addon(anki_session:AnkiSession):# profile / collection already pre-loaded!assertanki_session.collectionAdditional NotesWhen to use pytest-ankiRunning your test in an Anki environment is expensive and introduces an additional layer of confounding factors. If you canmockyour Anki runtime dependencies away, then that should always be your first tool of choice.Whereanki_sessioncomes in handy is further towards the upper levels of the test pyramid, i.e. functional tests, end-to-end tests, and UI tests. Additionally the plugin can provide you with a convenient way to automate testing for incompatibilities with Anki and other add-ons.The importance of forking your testsYou might have noticed that most of the examples above use a@pytest.mark.forkeddecorator. This is because, while the plugin does attempt to tear down Anki sessions as cleanly as possible on exit, this process is never quite perfect, especially for add-ons that monkey-patch Anki.With unforked test runs, factors like that can lead to unexpected behavior, or worse still, your tests crashing. Forking a new subprocess for each test bypasses these limitations, and therefore my advice would be to mark anyanki_sessiontests as forked by default.To do this in batch for an entire test module, you can use the following pytest hook:defpytest_collection_modifyitems(items):foriteminitems:item.add_marker("forked")Future versions ofpytest-ankiwill possibly do this by default.Automated Testingpytest-ankiis designed to work well with continuous integration systems such as GitHub actions. For an example seepytest-anki's ownGitHub workflows.Troubleshootingpytest hanging when using xvfbEspecially if you run your tests headlessly withxvfb, you might run into cases where pytest will sometimes appear to hang. Oftentimes this is due to blocking non-dismissable prompts that your add-on code might invoke in some scenarios. If you suspect that might be the case, my advice would be to temporarily bypassxvfblocally viapytest --no-xvfbto show the UI and manually debug the issue.ContributingContributions are welcome! To set uppytest-ankifor development, please first make sure you have Python 3.8+ andpoetryinstalled, then run the following steps:$ git clone https://github.com/glutanimate/pytest-anki.git

$ cd pytest-anki

# Either set up a new Python virtual environment at this stage
# (e.g. using pyenv), or let poetry create the venv for you

$ make installBefore submitting any changes, please make sure thatpytest-anki's checks and tests pass:makecheck
makelint
maketestThis project usesblack,isortandautoflaketo enforce a consistent code style. To auto-format your code you can use:makeformatLicense and Creditspytest-ankiisCopyright © 2019-2021Aristotelis P.(Glutanimate) andcontributorsCopyright © 2017-2019Michal KrassowskiCopyright © 2017-2021Ankitects Pty Ltd and contributorsAll credits for the original idea for creating a context manager to test Anki add-ons with go to Michal.pytest-ankiwould not exist without hisanki_testingproject.I would also like to extend a heartfelt thanks toAMBOSSfor their major part in supporting the development of this plugin! Most of the recent feature additions leading up to v1.0.0 of the plugin were implemented as part of my work on theAMBOSS add-on.pytest-ankiis free and open-source software. Its source-code is released under the GNU AGPLv3 license, extended by a number of additional terms. For more information please see thelicense filethat accompanies this program.This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Please see the license file for more details.