A Jupyter Notebook for Python's Behave
This is about a quick hack I did during PyCon Philippines 2016 and which I presented as a Lightning Talk.
Writing tests is daily bread for a software engineer these days. (We're going to be a real engineering discipline one day!) For example, in Python there's the basic unittest module, and other frameworks and test runners such as py.test or nose. Running those you can go a long way, even up to writing Selenium tests that start a browser like Firefox and test your actually deployed website.
Have a look at this example, written in BDD style.
@pytest.mark.slow @pytest.skip("Will be fixed in release 3.05, see github#3827") def test_(): tina = given_logged_in_user("Tina") when_user_changes_their_email(tina, new_mail="email@example.com") then_users_profile_shows(tina, email="firstname.lastname@example.org")
This is something that you don't want to show to your customers, no sir! Not even your PO. And it's about requirements; you should be discussing this with your PO. And for the same reason, this kind of test is not that great for documentation either. The holy grail, enabling your PO (and maybe even customers) to write and run their own tests … yeah, forget it. Running and nerding about testing is what these tests are about.
Readability and sharability is something frameworks like behave or pytest-bdd are built for. Using behave, the above could be written the following way.
Scenario: Email address is changed Given Tina is logged in. When Tina changes her email to email@example.com Then in her user profile Tina sees firstname.lastname@example.org as her email.
Arguably, this is something that you can discuss with your PO! Maybe even your customer. (Obviously, you can run this, too.)
But how about giving your PO the ability to run this? And is that really that great for documentation? For the latter, you can add comments … maybe generate a PDF out of this, I don't know. You could go for literate programming. Still, we want to run this easily!
That's what I thought about when I listened to Carol's talk about the Jupyter project and that it's now supporting not only Python, but a few dozen languages.
Wouldn't it be amazing to have your end to end tests as a Jupyter notebook? Changing and running them, documenting and discussing them right there?
To have Jupyter support a new language or environment you need to create a kernel. After a midnight hack I had a notebook running behave!
This is how it looks:
This is something you can share with anyone. Anyone can change it. Anyone can run it.
Not sure this is The Right Way (there are conceptual issues with behave). Maybe a custom written webapp is the way to go: A web frontend for the test runner plus a library search attached.
At any rate it's awesome that you can just do this and so quickly.
The proof of concept code for this Jupyter kernel is on github. Enjoy!