Get instance of Browser in unit-tests

Hello,

Trying to cover my addon code with PyTests.

Can’t realize how to instantiate aqt.browser.browser.Browser class.

The best example I can create is:

import os
import tempfile
from argparse import Namespace
from pathlib import Path

from aqt import AnkiQt, AnkiApp, ProfileManager, setupLangAndBackend
from aqt.browser import Browser

tmp_dir: str = tempfile.mkdtemp()
os.removedirs(tmp_dir)
base_dir: Path = ProfileManager.get_created_base_folder(tmp_dir)
pm: ProfileManager = ProfileManager(base=base_dir)
pm.setupMeta()
profile: str = "Profile1"
pm.create(profile)
pm.setLang("en")
pm.openProfile(profile)

args: list[str] = [f"base={base_dir}", f"profile={profile}"]
app: AnkiApp = AnkiApp(args)
app.startingUp()
namespace: Namespace = Namespace(
    safemode=False,
    profile=profile,
    base=base_dir,
    # path=None,
    web=False,
    # debug=False,
    # version=False,
    no_update_check=True
)
back = setupLangAndBackend(pm, app, None, False)
mw: AnkiQt = AnkiQt(app, pm, back, namespace, args)
mw.setupProfile()
browser: Browser = Browser(mw=mw)
# browser.show()
# browser.close()
# mw.close()

It fails with Process finished with exit code 139 (interrupted by signal 11:SIGSEGV).

aqt version is 25.09.02

Could you suggest a proper way to get Browser instance?

The test runs successfully for me locally on Windows.

You might need to use pytest-forked to avoid Qt crashes, especially if you plan to run tests on GitHub Actions.

I highly recommend giving pytest-anki a try. It’ll take of setting up Anki for you. The AnkiHub add-on has a lot of examples: ankihub_addon/tests/addon/test_integration.py at main · AnkiHubSoftware/ankihub_addon · GitHub

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.