.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/advanced/unit_testing.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_advanced_unit_testing.py: ===================== 7.8 testing your code ===================== This lesson describes unit testing in python. .. GENERATED FROM PYTHON SOURCE LINES 10-19 If you are writing a code which you think will not be used by you or anyone else in the future, then you can skip this lesson. However, if you intend to write a code, which will used by you and others in future then you must write tests for your code, so that your code is reliable. The purpose of writing tests is **to make sure that the code works and will keep on working the way it should work**. Consider the following function which adds two numbers .. GENERATED FROM PYTHON SOURCE LINES 19-22 .. code-block:: default def add(a,b): return a+b .. GENERATED FROM PYTHON SOURCE LINES 23-24 Now if we want to write a test for this function, it will look something like below .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: default summation = add(2,2) assert summation==4 .. GENERATED FROM PYTHON SOURCE LINES 29-31 Above we are testing the ``add`` function. But we have tested it only with integers as input. We should probably check it with float or other types as well. .. GENERATED FROM PYTHON SOURCE LINES 31-34 .. code-block:: default assert add(2.0, 2.0)==4.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.001 seconds) .. _sphx_glr_download_auto_examples_advanced_unit_testing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/advanced/unit_testing.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: unit_testing.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: unit_testing.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_