Here are some notes that we might need later.
If we want global learn_vision
, learn_text
, etc. objects, we can pre-create them from conftest.py
:
from fastai.vision import *
@pytest.fixture(scope="session", autouse=True)
def learn_vision():
path = untar_data(URLs.MNIST_TINY)
data = ImageDataBunch.from_folder(path, ds_tfms=(rand_pad(2, 28), []), num_workers=2)
data.normalize()
learn = Learner(data, simple_cnn((3,16,16,16,2), bn=True), metrics=[accuracy, error_rate])
learn.fit_one_cycle(3)
return learn
now inside let’s say tests/test_vision_train.py
I can access this via fixture:
def test_accuracy(learn_vision):
assert accuracy(*learn_vision.get_preds()) > 0.9
If we use:
@pytest.fixture(scope="session", autouse=True)
all global objects will be pre-created no matter whether the tests need them or not, so we probably don’t want autouse=True. w/o it these will be created on demand.
There is a cosmetic issue with having learn_vision
, learn_text
, since now we either have to spell out:
def test_accuracy(learn_vision):
assert accuracy(*learn_vision.get_preds()) > 0.9
or rename:
def test_accuracy(learn_vision):
learn = learn_vision
assert accuracy(*learn.get_preds()) > 0.9
both aren’t very great…
We want to be able to copy-n-paste quickly and ideally it should always be learn.foo
, especially since there are many calls usually.
Unrelated, with this and even with test module scoped variables (e.g. learn
) that we currently use there is an issue that tests can and will modify the object and the next test doesn’t get the same object but a modified one, so this can be very bad for tests, as it can mask serious issues.
Instead we need to have some kind of clone() method which will give each test a clean copy of the object.