Skip to content

Commit

Permalink
Merge pull request #4 from UWSEDS/update_testing_notebook
Browse files Browse the repository at this point in the history
Updating testing notebook for pytest
  • Loading branch information
bhazelton authored Nov 19, 2024
2 parents 5c5d84a + cb9f2e0 commit 1309399
Showing 1 changed file with 68 additions and 40 deletions.
108 changes: 68 additions & 40 deletions 07_debug_exceptions_testing/Unit-tests.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -78,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -273,11 +273,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def test_entropy_parameter_checking():\n",
"def entropy_parameter_checking():\n",
" # first, let's try not summing to 1\n",
" try:\n",
" entropy([.9, .9])\n",
Expand All @@ -295,7 +295,7 @@
"metadata": {},
"outputs": [],
"source": [
"test_entropy_parameter_checking()"
"entropy_parameter_checking()"
]
},
{
Expand All @@ -319,7 +319,7 @@
"outputs": [],
"source": [
"# Pattern test\n",
"def test_equal_probabilities(n):\n",
"def check_equal_probabilities(n):\n",
" prob = 1.0/n\n",
" ps = np.repeat(prob , n)\n",
" if np.isclose(entropy(ps), -np.log2(prob)):\n",
Expand All @@ -330,7 +330,7 @@
" \n",
" \n",
"# Run a test\n",
"test_equal_probabilities(100000)"
"check_equal_probabilities(100000)"
]
},
{
Expand All @@ -354,11 +354,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"def makeProbabilityMatrix(column_names, nrows):\n",
"def make_probability_matrix(column_names, nrows):\n",
" \"\"\"\n",
" Makes a dataframe with the specified column names such that each\n",
" cell is a value in [0, 1] and columns sum to 1.\n",
Expand Down Expand Up @@ -386,7 +386,7 @@
"metadata": {},
"outputs": [],
"source": [
"makeProbabilityMatrix(['a', 'b'], 3)"
"make_probability_matrix(['a', 'b'], 3)"
]
},
{
Expand All @@ -397,7 +397,7 @@
"source": [
"# Test 2: Check columns\n",
"COLUMNS = ['a', 'b']\n",
"df = makeProbabilityMatrix(COLUMNS, 3)\n",
"df = make_probability_matrix(COLUMNS, 3)\n",
"set(COLUMNS) == set(df.columns)"
]
},
Expand Down Expand Up @@ -429,13 +429,10 @@
"- The infrastructure provides a uniform way to report test results, and to handle test failures.\n",
"- A test infrastructure can tell you about coverage so you know what tests to add.\n",
"\n",
"We'll be using the `Pytest` framework. This is a separate Python package. Using this infrastructure, requires the following:\n",
"1. import the pytest module\n",
"We'll be using the `pytest` framework. This is a separate Python package. Using this infrastructure, requires the following:\n",
"1. write methods that run the code to be tested and check the outcomes.\n",
"\n",
"You indicate that a method is to be run as a test by having the method name begin with \"test\".\n",
"\n",
"Second, the \"test methods\" should communicate with the infrastructure the results of evaluating output from the code under test. This is done by using `assert` statements. For example, `np.testing.assert_equal` takes two arguments. If these are objects for which `==` returns `True`, then the test passes. Otherwise, the test fails."
"1. Indicate that a method is to be run as a test by having the method name begin with \"test\".\n",
"1. The \"test methods\" should communicate with the infrastructure the results of evaluating output from the code under test. This is done by using `assert` statements. For example, `np.testing.assert_equal` takes two arguments. If these are objects for which `==` returns `True`, then the test passes. Otherwise, the test fails."
]
},
{
Expand All @@ -455,26 +452,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"import pytest\n",
"import ipytest\n",
"import numpy as np\n",
"ipytest.autoconfig()"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%%ipytest\n",
"\n",
"# Define test functions\n",
"\n",
"def test_success():\n",
Expand All @@ -484,7 +478,16 @@
" assert 1 == 1\n",
"\n",
"def test_failure():\n",
" assert 1 ==2\n"
" assert 1 == 2\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ipytest.run()"
]
},
{
Expand Down Expand Up @@ -525,8 +528,6 @@
"outputs": [],
"source": [
"# Implementating a pattern test. Use functions in the test.\n",
"import pytest\n",
" \n",
"def test_equal_probability():\n",
" def test(count):\n",
" \"\"\"\n",
Expand All @@ -549,8 +550,6 @@
"metadata": {},
"outputs": [],
"source": [
"import pytest\n",
"\n",
"# Define test functions for the entropy function"
]
},
Expand All @@ -565,14 +564,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"%%ipytest\n",
"\n",
"import pytest\n",
"\n",
"# Define tests to run\n",
" \n",
"def test_invalid_probability():\n",
Expand All @@ -583,6 +578,15 @@
" assert True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ipytest.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -592,22 +596,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"%%ipytest\n",
"\n",
"import pytest\n",
"\n",
"# Define tests\n",
"\n",
"def test_invalid_probability():\n",
" with pytest.raises(ValueError):\n",
" with pytest.raises(ValueError, match=\"The list of input probabilities does not sum to 1\"):\n",
" entropy([0.1, 0.5])\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ipytest.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -638,11 +649,10 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"%%ipytest\n",
"import pytest\n",
"\n",
"# Define tests\n",
Expand All @@ -660,11 +670,29 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ipytest.run()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"#def geomean(argument?):\n",
"# return ?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ipytest.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -711,7 +739,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "cse583",
"language": "python",
"name": "python3"
},
Expand All @@ -725,7 +753,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.13.0"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 1309399

Please sign in to comment.