|
25 | 25 | "\n", |
26 | 26 | "**Unit testing** is a coding practice where tests are written to verify that your functions and classes work as intended. It helps ensure that your code behaves correctly across different scenarios and makes it easier to detect and fix bugs.\n", |
27 | 27 | "\n", |
28 | | - "In this tutorial, we will learn how to write documentation and unit tests for a function that calculates snowmelt, and practice these skills through hands-on exercises focused on a Diffusion Model class." |
| 28 | + "In this tutorial, we will learn how to write documentation and unit tests for a function that calculates snowmelt and practice these skills through hands-on exercises focused on a Diffusion Model class." |
29 | 29 | ] |
30 | 30 | }, |
31 | 31 | { |
|
57 | 57 | "\n", |
58 | 58 | "4. API documentation\n", |
59 | 59 | "\n", |
60 | | - " Detailed descriptions of classes, functions, methods, and modules. It is typically generated using tools (e.g., [Sphinx](https://www.sphinx-doc.org/en/master/)) that extract information from docstrings written for the functions, classes, and modules. ([API doc example](https://landlab.readthedocs.io/en/v2.9.2/generated/api/landlab.components.overland_flow.generate_overland_flow_deAlmeida.html))\n", |
| 60 | + " Detailed descriptions of classes, functions, methods, and modules. It is typically generated using tools (e.g., [Sphinx](https://www.sphinx-doc.org/en/master/)) that extract information from docstrings written for the functions, classes, and modules. ([API documentation example](https://landlab.readthedocs.io/en/v2.9.2/generated/api/landlab.components.overland_flow.generate_overland_flow_deAlmeida.html))\n", |
61 | 61 | "\n", |
62 | 62 | "5. Tutorials and Examples\n", |
63 | 63 | "\n", |
64 | | - " Code examples, Jupyter notebooks, or step-by-step guides for using the software. ([Tutorial example](https://github.com/landlab/landlab/blob/master/docs/source/tutorials/boundary_conditions/set_BCs_on_raster_perimeter.ipynb))" |
| 64 | + " Code examples, Jupyter notebooks, or step-by-step guides for using the software. ([Tutorial example](https://landlab.readthedocs.io/en/v2.9.2/user_guide/overland_flow_user_guide.html))" |
65 | 65 | ] |
66 | 66 | }, |
67 | 67 | { |
|
82 | 82 | "source": [ |
83 | 83 | "The example code below shows the calculate_snowmelt() function. Its documentation follows the NumPy-style docstring format, which is widely adopted by major scientific libraries such as NumPy, SciPy, and pandas. This style is especially common in scientific and data-analysis code.\n", |
84 | 84 | "\n", |
85 | | - "In the documentation, the Examples section provides illustrative use cases of the function. These examples can also serve as the basis for unit tests, helping to verify that the function behaves as expected. We will discuss this further in a later section." |
| 85 | + "In the documentation, the `Examples` section provides illustrative use cases of the function. These examples can also serve as the basis for unit tests, helping to verify that the function behaves as expected. We will discuss this further in the unit testing section." |
86 | 86 | ] |
87 | 87 | }, |
88 | 88 | { |
|
99 | 99 | " Parameters\n", |
100 | 100 | " ----------\n", |
101 | 101 | " temp : float\n", |
102 | | - " Daily average temperature in Celsius.\n", |
| 102 | + " Daily average temperature in °C.\n", |
103 | 103 | " degree_day_factor : float\n", |
104 | 104 | " Melt factor in mm/°C/day.\n", |
105 | 105 | " days : int, optional\n", |
|
140 | 140 | "id": "9", |
141 | 141 | "metadata": {}, |
142 | 142 | "source": [ |
143 | | - "### Frameworks for unit testing\n", |
144 | | - "Python offers several tools for writing and running tests. Three popular testing frameworks are doctest, unittest, and pytest. Each has its own strengths and is suited to different testing needs. - \n", |
| 143 | + "### Tools for unit testing\n", |
| 144 | + "Python offers several tools for writing and running tests. Three commonly used testing tools are doctest, unittest, and pytest. Each has its own strengths and is suited to different testing needs.\n", |
145 | 145 | "- **doctest**: A standard Python library designed to test code examples embedded in docstrings. It is not a full-featured unit testing framework. But it’s ideal for verifying simple functions and ensuring that documentation remains accurate and executable.\n", |
146 | 146 | "- **unittest**: A standard Python library that provides a framework for writing and running unit tests. It best suited for organizing larger test suites using test classes and methods.\n", |
147 | 147 | "- **pytest**: A powerful third-party testing framework for writing and running tests. It is more flexible and powerful than unittest, supporting both standalone test functions and test classes. It also supports advanced features (e.g., parameterization, fixture) making it suitable for both simple and complex testing needs.\n", |
|
157 | 157 | "We will use pytest to write the unit tests for the \"calculate_snowmelt( )\" function. Let's first think about what testing cases are needed for this function:\n", |
158 | 158 | " - snowmelt occurs with valid input\n", |
159 | 159 | " - no snowmelt with valid input\n", |
160 | | - " - invalide degree_day_factor input\n", |
161 | | - " - invalid day input" |
| 160 | + " - invalid degree_day_factor input\n", |
| 161 | + " - invalid days input" |
162 | 162 | ] |
163 | 163 | }, |
164 | 164 | { |
|
178 | 178 | "metadata": {}, |
179 | 179 | "outputs": [], |
180 | 180 | "source": [ |
181 | | - "import pytest\n", |
182 | | - "\n", |
183 | | - "\n", |
184 | 181 | "# test snowmelt occurs\n", |
185 | 182 | "def test_snowmelt_occurs():\n", |
186 | 183 | " \"\"\"Test that snowmelt occurs with valid input\"\"\"\n", |
|
243 | 240 | "metadata": {}, |
244 | 241 | "outputs": [], |
245 | 242 | "source": [ |
246 | | - "# test invalid day input" |
| 243 | + "# test invalid days input" |
247 | 244 | ] |
248 | 245 | }, |
249 | 246 | { |
|
280 | 277 | "If you know about [conda](https://docs.conda.io/projects/conda/en/stable/) and have installed it on your computer, you can follow the steps below to create the function file and the unit test file, and then create a conda environment for running the unit tests.\n", |
281 | 278 | "\n", |
282 | 279 | "1) Create a new folder named \"example\". Inside this folder, you’ll place your code and test files.\n", |
283 | | - "2) Create \"example.py\": paste the code for calculate_snowmelt() in this file.\n", |
284 | | - "3) Create \"test_example.py\": paste the unit test functions in this file. Remember to add oneline of code in this file:\n", |
| 280 | + "2) Create \"example.py\": paste the code for calculate_snowmelt() function in this file.\n", |
| 281 | + "3) Create \"test_example.py\": paste the four unit test functions in this file. Remember to add the code below in this file:\n", |
285 | 282 | "```python\n", |
| 283 | + "import pytest\n", |
| 284 | + "\n", |
286 | 285 | "from example import calculate_snowmelt\n", |
287 | 286 | "```\n", |
288 | | - "4) Open a terminal, create a conda environment and activate it with the following commands. Remember to replace /path/to/your/diffusion with the actual path.\n", |
| 287 | + "4) Open a terminal, create a conda environment and activate it with the following commands. Remember to replace /path/to/your/example_folder with the actual path.\n", |
289 | 288 | "```bash\n", |
290 | | - "conda create -n unit_test pytest\n", |
291 | | - "conda activate unit_test\n", |
| 289 | + "conda create -y -n unit_testing pytest\n", |
| 290 | + "conda activate unit_testing\n", |
292 | 291 | "cd /path/to/your/example_folder\n", |
293 | 292 | "```" |
294 | 293 | ] |
|
320 | 319 | "```\n", |
321 | 320 | "**doctest with incorrct example**\n", |
322 | 321 | "```\n", |
323 | | - "$ python -m doctest example.py\n", |
| 322 | + "$ python -m doctest example_wrong.py\n", |
324 | 323 | "**********************************************************************\n", |
325 | 324 | "File \"/Users/tiga7385/Desktop/course/example/example.py\", line 27, in ex\n", |
326 | 325 | "ample.calculate_snowmelt\n", |
|
684 | 683 | " pass # remove pass and add your code\n", |
685 | 684 | "\n", |
686 | 685 | "\n", |
687 | | - "# feel free to add additional test functions" |
| 686 | + "# feel free to add additional unit test functions" |
688 | 687 | ] |
689 | 688 | }, |
690 | 689 | { |
|
747 | 746 | "id": "34", |
748 | 747 | "metadata": {}, |
749 | 748 | "source": [ |
750 | | - "**3) Bonus Exercise: Run Unit Tests**\n", |
| 749 | + "**3) Run Unit Tests**\n", |
751 | 750 | "\n", |
752 | 751 | "Follow the steps shown in the \"Run Unit Tests\" section to run the unit tests for the Diffution2D class on your computer. \n", |
753 | 752 | "\n", |
|
0 commit comments