Skip to content

Commit 1309399

Browse files
authored
Merge pull request #4 from UWSEDS/update_testing_notebook
Updating testing notebook for pytest
2 parents 5c5d84a + cb9f2e0 commit 1309399

File tree

1 file changed

+68
-40
lines changed

1 file changed

+68
-40
lines changed

07_debug_exceptions_testing/Unit-tests.ipynb

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
{
1414
"cell_type": "code",
15-
"execution_count": null,
15+
"execution_count": 1,
1616
"metadata": {},
1717
"outputs": [],
1818
"source": [
@@ -78,7 +78,7 @@
7878
},
7979
{
8080
"cell_type": "code",
81-
"execution_count": null,
81+
"execution_count": 2,
8282
"metadata": {},
8383
"outputs": [],
8484
"source": [
@@ -273,11 +273,11 @@
273273
},
274274
{
275275
"cell_type": "code",
276-
"execution_count": null,
276+
"execution_count": 11,
277277
"metadata": {},
278278
"outputs": [],
279279
"source": [
280-
"def test_entropy_parameter_checking():\n",
280+
"def entropy_parameter_checking():\n",
281281
" # first, let's try not summing to 1\n",
282282
" try:\n",
283283
" entropy([.9, .9])\n",
@@ -295,7 +295,7 @@
295295
"metadata": {},
296296
"outputs": [],
297297
"source": [
298-
"test_entropy_parameter_checking()"
298+
"entropy_parameter_checking()"
299299
]
300300
},
301301
{
@@ -319,7 +319,7 @@
319319
"outputs": [],
320320
"source": [
321321
"# Pattern test\n",
322-
"def test_equal_probabilities(n):\n",
322+
"def check_equal_probabilities(n):\n",
323323
" prob = 1.0/n\n",
324324
" ps = np.repeat(prob , n)\n",
325325
" if np.isclose(entropy(ps), -np.log2(prob)):\n",
@@ -330,7 +330,7 @@
330330
" \n",
331331
" \n",
332332
"# Run a test\n",
333-
"test_equal_probabilities(100000)"
333+
"check_equal_probabilities(100000)"
334334
]
335335
},
336336
{
@@ -354,11 +354,11 @@
354354
},
355355
{
356356
"cell_type": "code",
357-
"execution_count": null,
357+
"execution_count": 14,
358358
"metadata": {},
359359
"outputs": [],
360360
"source": [
361-
"def makeProbabilityMatrix(column_names, nrows):\n",
361+
"def make_probability_matrix(column_names, nrows):\n",
362362
" \"\"\"\n",
363363
" Makes a dataframe with the specified column names such that each\n",
364364
" cell is a value in [0, 1] and columns sum to 1.\n",
@@ -386,7 +386,7 @@
386386
"metadata": {},
387387
"outputs": [],
388388
"source": [
389-
"makeProbabilityMatrix(['a', 'b'], 3)"
389+
"make_probability_matrix(['a', 'b'], 3)"
390390
]
391391
},
392392
{
@@ -397,7 +397,7 @@
397397
"source": [
398398
"# Test 2: Check columns\n",
399399
"COLUMNS = ['a', 'b']\n",
400-
"df = makeProbabilityMatrix(COLUMNS, 3)\n",
400+
"df = make_probability_matrix(COLUMNS, 3)\n",
401401
"set(COLUMNS) == set(df.columns)"
402402
]
403403
},
@@ -429,13 +429,10 @@
429429
"- The infrastructure provides a uniform way to report test results, and to handle test failures.\n",
430430
"- A test infrastructure can tell you about coverage so you know what tests to add.\n",
431431
"\n",
432-
"We'll be using the `Pytest` framework. This is a separate Python package. Using this infrastructure, requires the following:\n",
433-
"1. import the pytest module\n",
432+
"We'll be using the `pytest` framework. This is a separate Python package. Using this infrastructure, requires the following:\n",
434433
"1. write methods that run the code to be tested and check the outcomes.\n",
435-
"\n",
436-
"You indicate that a method is to be run as a test by having the method name begin with \"test\".\n",
437-
"\n",
438-
"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."
434+
"1. Indicate that a method is to be run as a test by having the method name begin with \"test\".\n",
435+
"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."
439436
]
440437
},
441438
{
@@ -455,26 +452,23 @@
455452
},
456453
{
457454
"cell_type": "code",
458-
"execution_count": null,
455+
"execution_count": 17,
459456
"metadata": {},
460457
"outputs": [],
461458
"source": [
462-
"import pytest\n",
463459
"import ipytest\n",
464460
"import numpy as np\n",
465461
"ipytest.autoconfig()"
466462
]
467463
},
468464
{
469465
"cell_type": "code",
470-
"execution_count": null,
466+
"execution_count": 18,
471467
"metadata": {
472468
"scrolled": true
473469
},
474470
"outputs": [],
475471
"source": [
476-
"%%ipytest\n",
477-
"\n",
478472
"# Define test functions\n",
479473
"\n",
480474
"def test_success():\n",
@@ -484,7 +478,16 @@
484478
" assert 1 == 1\n",
485479
"\n",
486480
"def test_failure():\n",
487-
" assert 1 ==2\n"
481+
" assert 1 == 2\n"
482+
]
483+
},
484+
{
485+
"cell_type": "code",
486+
"execution_count": null,
487+
"metadata": {},
488+
"outputs": [],
489+
"source": [
490+
"ipytest.run()"
488491
]
489492
},
490493
{
@@ -525,8 +528,6 @@
525528
"outputs": [],
526529
"source": [
527530
"# Implementating a pattern test. Use functions in the test.\n",
528-
"import pytest\n",
529-
" \n",
530531
"def test_equal_probability():\n",
531532
" def test(count):\n",
532533
" \"\"\"\n",
@@ -549,8 +550,6 @@
549550
"metadata": {},
550551
"outputs": [],
551552
"source": [
552-
"import pytest\n",
553-
"\n",
554553
"# Define test functions for the entropy function"
555554
]
556555
},
@@ -565,14 +564,10 @@
565564
},
566565
{
567566
"cell_type": "code",
568-
"execution_count": null,
567+
"execution_count": 21,
569568
"metadata": {},
570569
"outputs": [],
571570
"source": [
572-
"%%ipytest\n",
573-
"\n",
574-
"import pytest\n",
575-
"\n",
576571
"# Define tests to run\n",
577572
" \n",
578573
"def test_invalid_probability():\n",
@@ -583,6 +578,15 @@
583578
" assert True"
584579
]
585580
},
581+
{
582+
"cell_type": "code",
583+
"execution_count": null,
584+
"metadata": {},
585+
"outputs": [],
586+
"source": [
587+
"ipytest.run()"
588+
]
589+
},
586590
{
587591
"cell_type": "markdown",
588592
"metadata": {},
@@ -592,22 +596,29 @@
592596
},
593597
{
594598
"cell_type": "code",
595-
"execution_count": null,
599+
"execution_count": 24,
596600
"metadata": {},
597601
"outputs": [],
598602
"source": [
599-
"%%ipytest\n",
600-
"\n",
601603
"import pytest\n",
602604
"\n",
603605
"# Define tests\n",
604606
"\n",
605607
"def test_invalid_probability():\n",
606-
" with pytest.raises(ValueError):\n",
608+
" with pytest.raises(ValueError, match=\"The list of input probabilities does not sum to 1\"):\n",
607609
" entropy([0.1, 0.5])\n",
608610
" \n"
609611
]
610612
},
613+
{
614+
"cell_type": "code",
615+
"execution_count": null,
616+
"metadata": {},
617+
"outputs": [],
618+
"source": [
619+
"ipytest.run()"
620+
]
621+
},
611622
{
612623
"cell_type": "markdown",
613624
"metadata": {},
@@ -638,11 +649,10 @@
638649
},
639650
{
640651
"cell_type": "code",
641-
"execution_count": null,
652+
"execution_count": 26,
642653
"metadata": {},
643654
"outputs": [],
644655
"source": [
645-
"%%ipytest\n",
646656
"import pytest\n",
647657
"\n",
648658
"# Define tests\n",
@@ -660,11 +670,29 @@
660670
"execution_count": null,
661671
"metadata": {},
662672
"outputs": [],
673+
"source": [
674+
"ipytest.run()"
675+
]
676+
},
677+
{
678+
"cell_type": "code",
679+
"execution_count": 9,
680+
"metadata": {},
681+
"outputs": [],
663682
"source": [
664683
"#def geomean(argument?):\n",
665684
"# return ?"
666685
]
667686
},
687+
{
688+
"cell_type": "code",
689+
"execution_count": null,
690+
"metadata": {},
691+
"outputs": [],
692+
"source": [
693+
"ipytest.run()"
694+
]
695+
},
668696
{
669697
"cell_type": "markdown",
670698
"metadata": {},
@@ -711,7 +739,7 @@
711739
],
712740
"metadata": {
713741
"kernelspec": {
714-
"display_name": "Python 3 (ipykernel)",
742+
"display_name": "cse583",
715743
"language": "python",
716744
"name": "python3"
717745
},
@@ -725,7 +753,7 @@
725753
"name": "python",
726754
"nbconvert_exporter": "python",
727755
"pygments_lexer": "ipython3",
728-
"version": "3.11.5"
756+
"version": "3.13.0"
729757
}
730758
},
731759
"nbformat": 4,

0 commit comments

Comments
 (0)