Hi everyone! If you’re not aware, Jeremy and Hamel are heavily working on version 2 of nbdev, nbprocess. This framework looked exceptionally interesting to me because its simple, and most of all modular.
What do I mean? I took it out for a test drive this afternoon, and in no more than 5 hours I was able to have nbdev automatically export your tests as pytest modules!.
This is a screenshot from my nbprocess sandbox where I’ll be toying around with a few ideas as we go.
So, how does this work then Zach?
Right now it’s very limited, but you’ll get the idea.
- We only have one
unittest.TestCase
per notebook. (It’s a POC, cut me at least a little slack please ) - We assume that all tests related to a particular functionality will reside in the same cell. Or at least one test we want as a pytest.
- All we need to do is declare what is a test!
For example, here are two cells (separated out by ---
)
#|export
def addition(a,b): return a+b
-------------------------------------
#|test addition
test_eq(addition(1,2), 3)
test_eq(addition(-1,0), -1)
That first #|export
is how we export things in nbprocess
now. It’s a bit of a nifty superpower that lets us pull off our next magic trick.
That second one, the #|test
portion tells nbprocess we want to mark this as a “test”. The second is what I have deemed to be the name of said test, “addition
”.
From here, you just perform the following (if you pip install from my repo):
from testing_grounds.export import create_test_modules
create_test_modules(some_notebook_name, "tests")
This will then create that beautiful pytest tests/test_core.py
we saw earlier. All at the touch of your fingertips!
Currently it just keeps the fastcore testing logic and relies on those asserts, but it’s not hard to imagine being able to use pytest’s self.assertXYZ
along the way.
Hope you enjoyed this brief insight to what’s coming, and that you’re just as excited as I am