An open API service indexing awesome lists of open source software.

https://github.com/deathbeds/sweet

Testing for interactive programming
https://github.com/deathbeds/sweet

doctest hypothesis ipython jupyter testing unittest

Last synced: about 1 month ago
JSON representation

Testing for interactive programming

Awesome Lists containing this project

README

        

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# testing and typing is __Sweet__\n",
" \n",
"[![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/deathbeds/sweet/master?filepath=readme.ipynb) \n",
" \n",
"__Sweet__ is an easy way to test interactive code. It combines unittest, doctest, and hypothesis to promoting better code health."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
" if __name__ == '__main__':\n",
" %reload_ext sweet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Because creating a function or class should do more than create a name."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
" def test_no_params():\n",
" \"\"\"This function is tested because it has no parameters. It is executed using FunctionTestCase\n",
"\n",
" >>> assert True\n",
"\n",
" The docstring is also tested.\n",
" \"\"\"\n",
" assert True"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
" ct = 0\n",
" def when_there_are_annotations(x: int):\n",
" global ct\n",
" ct +=1\n",
" return x"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
" def after_hypothesis():\n",
" global ct\n",
" assert ct > 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Interactive typing \n",
"\n",
"Using [__monkeytype__]()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The sweet.typing extension is already loaded. To reload it, use:\n",
" %reload_ext sweet.typing\n"
]
},
{
"data": {
"text/plain": [
"from pandas.core.frame import DataFrame\n",
"from typing import Union\n",
"\n",
"\n",
"def after_hypothesis() -> None: ...\n",
"\n",
"\n",
"def f(x: Union[float, DataFrame, str, int]) -> str: ..."
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" %load_ext sweet.typing\n",
" def f(x):\n",
" return str(x)\n",
" %typing\n",
" f(10)\n",
" f(10.)\n",
" f('asdf')\n",
" f(__import__('pandas').util.testing.makeDataFrame())\n",
" after_hypothesis()\n",
" %typing readme.pyi\n",
" __import__('IPython').display.Pretty(filename='readme.pyi')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Developer."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook readme.ipynb to markdown\n",
"[NbConvertApp] Writing 2016 bytes to readme.md\n",
"The readme shows the \n",
"and the source shows the \n",
"🏆\n"
]
}
],
"source": [
" if __name__ == '__main__':\n",
" !jupyter nbconvert --to markdown readme.ipynb\n",
" from sweet import Sweet\n",
" result = Sweet().run()\n",
" print(f\"\"\"The readme shows the {result}\"\"\")\n",
" print(f\"\"\"and the source shows the {Sweet(module='sweet').run(result)}\"\"\")\n",
" print(\"🏆\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "p6",
"language": "python",
"name": "other-env"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}