switch from nose2 to pytest

pytest will show what differs in simple assert statements

concurrent mode is supported with a plugin, but like nose2, concurrent
mode hides the cause of import errors, so I've left it off for now.
This commit is contained in:
Damien Elmes 2020-01-03 08:52:10 +10:00
parent c9da813622
commit 1070c866f3
7 changed files with 15 additions and 41 deletions

View file

@ -55,7 +55,7 @@ CHECKDEPS := $(shell find anki tests -name '*.py' | grep -v buildhash.py)
@touch $@ @touch $@
.build/test: $(CHECKDEPS) .build/test: $(CHECKDEPS)
python -m nose2 --plugin=nose2.plugins.mp -N 16 python -m pytest
@touch $@ @touch $@
.build/lint: $(CHECKDEPS) .build/lint: $(CHECKDEPS)

View file

@ -2,7 +2,7 @@ wheel
mypy mypy
mypy_protobuf mypy_protobuf
black black
nose2 pytest
# fixme: when isort 5.0 is released, switch to pypi # fixme: when isort 5.0 is released, switch to pypi
git+https://github.com/ankitects/isort#egg=isort git+https://github.com/ankitects/isort#egg=isort
# fixme: when pylint supports isort 5.0, switch to pypi # fixme: when pylint supports isort 5.0, switch to pypi

View file

@ -3,8 +3,6 @@
import os import os
import tempfile import tempfile
from nose2.tools.decorators import with_setup
from anki import Collection as aopen from anki import Collection as aopen
from anki.exporting import * from anki.exporting import *
from anki.importing import Anki2Importer from anki.importing import Anki2Importer
@ -41,8 +39,8 @@ def setup1():
########################################################################## ##########################################################################
@with_setup(setup1)
def test_export_anki(): def test_export_anki():
setup1()
# create a new deck with its own conf to test conf copying # create a new deck with its own conf to test conf copying
did = deck.decks.id("test") did = deck.decks.id("test")
dobj = deck.decks.get(did) dobj = deck.decks.get(did)
@ -83,8 +81,8 @@ def test_export_anki():
assert d2.cardCount() == 1 assert d2.cardCount() == 1
@with_setup(setup1)
def test_export_ankipkg(): def test_export_ankipkg():
setup1()
# add a test file to the media folder # add a test file to the media folder
with open(os.path.join(deck.media.dir(), "今日.mp3"), "w") as f: with open(os.path.join(deck.media.dir(), "今日.mp3"), "w") as f:
f.write("test") f.write("test")
@ -99,8 +97,8 @@ def test_export_ankipkg():
e.exportInto(newname) e.exportInto(newname)
@with_setup(setup1)
def test_export_anki_due(): def test_export_anki_due():
setup1()
deck = getEmptyCol() deck = getEmptyCol()
f = deck.newNote() f = deck.newNote()
f["Front"] = "foo" f["Front"] = "foo"
@ -132,8 +130,8 @@ def test_export_anki_due():
assert c.due - deck2.sched.today == 1 assert c.due - deck2.sched.today == 1
# @with_setup(setup1)
# def test_export_textcard(): # def test_export_textcard():
# setup1()
# e = TextCardExporter(deck) # e = TextCardExporter(deck)
# f = unicode(tempfile.mkstemp(prefix="ankitest")[1]) # f = unicode(tempfile.mkstemp(prefix="ankitest")[1])
# os.unlink(f) # os.unlink(f)
@ -142,8 +140,8 @@ def test_export_anki_due():
# e.exportInto(f) # e.exportInto(f)
@with_setup(setup1)
def test_export_textnote(): def test_export_textnote():
setup1()
e = TextNoteExporter(deck) e = TextNoteExporter(deck)
fd, f = tempfile.mkstemp(prefix="ankitest") fd, f = tempfile.mkstemp(prefix="ankitest")
f = str(f) f = str(f)

View file

@ -1,5 +1,5 @@
# coding: utf-8 # coding: utf-8
from nose2.tools.such import helper import pytest
from anki.find import Finder from anki.find import Finder
from tests.shared import getEmptyCol from tests.shared import getEmptyCol
@ -111,7 +111,7 @@ def test_findCards():
assert len(deck.findCards("nid:%d" % f.id)) == 2 assert len(deck.findCards("nid:%d" % f.id)) == 2
assert len(deck.findCards("nid:%d,%d" % (f1id, f2id))) == 2 assert len(deck.findCards("nid:%d,%d" % (f1id, f2id))) == 2
# templates # templates
with helper.assertRaises(Exception): with pytest.raises(Exception):
deck.findCards("card:foo") deck.findCards("card:foo")
assert len(deck.findCards("'card:card 1'")) == 4 assert len(deck.findCards("'card:card 1'")) == 4
assert len(deck.findCards("card:reverse")) == 1 assert len(deck.findCards("card:reverse")) == 1
@ -147,7 +147,7 @@ def test_findCards():
assert len(deck.findCards("-deck:foo")) == 5 assert len(deck.findCards("-deck:foo")) == 5
assert len(deck.findCards("deck:def*")) == 5 assert len(deck.findCards("deck:def*")) == 5
assert len(deck.findCards("deck:*EFAULT")) == 5 assert len(deck.findCards("deck:*EFAULT")) == 5
with helper.assertRaises(Exception): with pytest.raises(Exception):
deck.findCards("deck:*cefault") deck.findCards("deck:*cefault")
# full search # full search
f = deck.newNote() f = deck.newNote()
@ -164,7 +164,7 @@ def test_findCards():
# assert len(deck.findCards("helloworld", full=True)) == 2 # assert len(deck.findCards("helloworld", full=True)) == 2
# assert len(deck.findCards("back:helloworld", full=True)) == 2 # assert len(deck.findCards("back:helloworld", full=True)) == 2
# searching for an invalid special tag should not error # searching for an invalid special tag should not error
with helper.assertRaises(Exception): with pytest.raises(Exception):
len(deck.findCards("is:invalid")) len(deck.findCards("is:invalid"))
# should be able to limit to parent deck, no children # should be able to limit to parent deck, no children
id = deck.db.scalar("select id from cards limit 1") id = deck.db.scalar("select id from cards limit 1")
@ -238,9 +238,9 @@ def test_findCards():
assert len(deck.findCards("added:1")) == deck.cardCount() - 1 assert len(deck.findCards("added:1")) == deck.cardCount() - 1
assert len(deck.findCards("added:2")) == deck.cardCount() assert len(deck.findCards("added:2")) == deck.cardCount()
# flag # flag
with helper.assertRaises(Exception): with pytest.raises(Exception):
deck.findCards("flag:01") deck.findCards("flag:01")
with helper.assertRaises(Exception): with pytest.raises(Exception):
deck.findCards("flag:12") deck.findCards("flag:12")

View file

@ -75,7 +75,7 @@ CHECKDEPS := $(shell find aqt tests -name '*.py')
@touch $@ @touch $@
.build/test: $(CHECKDEPS) .build/test: $(CHECKDEPS)
python -m nose2 --plugin=nose2.plugins.mp -N 16 python -m pytest
@touch $@ @touch $@
.build/lint: $(CHECKDEPS) .build/lint: $(CHECKDEPS)

View file

@ -3,7 +3,6 @@ from tempfile import TemporaryDirectory
from zipfile import ZipFile from zipfile import ZipFile
from mock import MagicMock from mock import MagicMock
from nose2.tools.such import helper
from aqt.addons import AddonManager from aqt.addons import AddonManager
@ -64,4 +63,4 @@ def assertReadManifest(contents, expectedManifest, nameInZip="manifest.json"):
adm = AddonManager(MagicMock()) adm = AddonManager(MagicMock())
with ZipFile(zfn, "r") as zfile: with ZipFile(zfn, "r") as zfile:
helper.assertEquals(adm.readManifestFile(zfile), expectedManifest) assert adm.readManifestFile(zfile) == expectedManifest

View file

@ -1,23 +0,0 @@
#!/bin/bash
#
# Usage:
# tools/tests.sh # run all tests
# tools/tests.sh decks # test only test_decks.py
# coverage=1 tools/tests.sh # run with coverage test
set -e
BIN="$(cd "`dirname "$0"`"; pwd)"
export PYTHONPATH=${BIN}/..:${PYTHONPATH}
nose="python -m nose2 --plugin=nose2.plugins.mp -N 16"
dir=.
if [ x$1 = x ]; then
lim="tests"
else
lim="tests.test_$1"
fi
(cd $dir && $nose $lim $args)