@@ -478,45 +478,53 @@ Now whenever you call `mock_version.method_to_mock()` the return value will be `
478
478
::::::::::::::::::::::::::::::::::::::: challenge
479
479
480
480
## Exercise: Test Using a Mock Implementation
481
+ Add a new test file called ` test_compute_data.py ` in the tests folder and add a test to verify
482
+ whether we can successfully run ` analyse_data() ` when passing it a data source.
481
483
482
- Complete this test for ` analyse_data() ` , using a mock object in place of the
483
- ` data_source ` :
484
+ Complete this test for ` analyse_data() ` , using a mock object in place of the ` data_source ` :
484
485
485
486
``` python
486
487
from unittest.mock import Mock
487
488
488
- def test_compute_data_mock_source ():
489
+ def test_analyse_data_mock_source ():
489
490
from inflammation.compute_data import analyse_data
490
491
data_source = Mock()
491
492
492
493
# TODO : configure data_source mock
493
494
494
- result = analyse_data(data_source)
495
+ analyse_data(data_source)
495
496
496
- # TODO : add assert on the contents of result
497
497
```
498
498
499
499
Create a mock that returns some fixed data and to use as the ` data_source ` in order to test
500
500
the ` analyse_data ` method.
501
- Use this mock in a test.
501
+
502
+ Use this mock in the test.
502
503
503
504
Do not forget to import ` Mock ` from the ` unittest.mock ` package.
504
505
506
+ Note that the ` analyse_data() ` function visualizes the data with ` views.visualize(graph_data) ` .
507
+ You do not have to assert that this is done correctly. For now, it is fine to just check that
508
+ the call to ` analyse_data() ` can proceed successfully.
509
+
510
+ In the next episode we will adapt the ` analyse_data() ` function
511
+ so that we can write a test that asserts whether standard deviation calculations are correct.
512
+
505
513
::::::::::::::: solution
506
514
507
515
## Solution
508
516
509
517
``` python
510
518
from unittest.mock import Mock
511
519
512
- def test_compute_data_mock_source ():
520
+ def test_analyse_data_mock_source ():
513
521
from inflammation.compute_data import analyse_data
514
522
data_source = Mock()
515
- data_source.load_inflammation_data.return_value = [[[0 , 2 , 0 ]],
516
- [[0 , 1 , 0 ]]]
523
+ mock_data = [[[0 , 2 , 0 ]],
524
+ [[0 , 1 , 0 ]]]
525
+ data_source.load_inflammation_data.return_value = mock_data
517
526
518
- result = analyse_data(data_source)
519
- npt.assert_array_almost_equal(result, [0 , math.sqrt(0.25 ) ,0 ])
527
+ analyse_data(data_source)
520
528
```
521
529
522
530
:::::::::::::::::::::::::
@@ -526,8 +534,8 @@ def test_compute_data_mock_source():
526
534
## Safe Code Structure Changes
527
535
528
536
With the changes to the code structure we have done using code decoupling and abstractions we have
529
- already refactored our code to a certain extent but we have not tested that the changes work as
530
- intended.
537
+ already refactored our code to a certain extent,
538
+ but we have not fully tested that the changes work as intended.
531
539
We will now look into how to properly refactor code to guarantee that the code still works
532
540
as before any modifications.
533
541
0 commit comments