Effective Python Classroom Series – 06/Dec/2021

Bugs in Software Development Life Cycle(SDLC)

  • If the bug is found
    • In Production By customers, organization has to spend lot of time and effort as the new version has to be released Preview
    • If we find bugs and if they are reported by our testing teams in any of the test environment, This would be better than bugs found at production Preview
    • If the bug is found on the developers sytem, this would be best case as developer will fix it immedietly and cost of fixing bug is much low Preview
  • Organization would expect bugs to be found in earlier stages of SDLC a

Unit Testing

  • Unittest is a software testing method in which individual components of the program called as unit are tested independently.
  • Unittesting is mostly done by programmers who write the programs.
  • Unittesting can be done
    • by manually inspecting/debugging the code
    • Automated tests => these tests run every time on the build servers and give feedback to the developer
  • Approaches to be followed:
    • Regression: This is an testing approach teo ensure new changes will not break the existing functionality
    • Unit Tests should be executed on every developers system during changes are being made and also by the build server which tries to build the python packages, executes unit tests and shares the feedback & many more

Developer Setup

  • Python
  • Git:
  • Visual Studio:
  • Ensure python extension is installed Preview

DocStrings

  • Zen of Python (PEP-20) Refer Here defies the basic principle while writing python code
  • Naming & Styling Guide PEP-8 Refer Here
  • PEPs related Doc strings
  • In most enterprises, the Google way of doc string are adopted Refer Here
  • Now lets create the calc.py and add a function to add two numbers Preview
  • Refer Here to view the code changes

Doctest

  • This is a lightweight unit-testing framework in python that uses docstring to test automation.
  • doctest is packaged with Python interpreter (part of your python installation)
  • Python comes with batteries included philosophy
  • now using doctest, lets try to unittest
python -m doctest -v calc.py
  • Using doctest we can run the unittest without writing any as doctest parses the docstring and generates the tests accordingly
  • Refer Here for the changes made for the doctest to automatically unittest the code

unittest

  • Python used to have a module which was called PyUnit.
  • PyUnit was the Python port JUnit. Its Java xUnit style unit test automation Refer Here for xunit
  • xUnit is the collective name for several unit testing frameworks.
  • All xUnit-style testing framework more or less derive their functionality, structure and coding style from Smalltalk’s unit tesing framework sUnit.
  • PyUnit became part of Python Standard Library from 2.5 and it was renamed to unittest.
  • So lets understand this unittest module
  • All the xUnit-style test automation libraries follow th common architecture. The major components of the architecture
    • Test case class:
      • This is the base class of all the test class in the test modules
    • Test fixtures:
      • These are the functions or methods that run before and after blcoks of the test code execute
    • Assertions:
      • These functions or methods are used to check the behavior of the component being tested
    • Test suite:
      • Collection of all the related tests to be executed
    • Test runner: This is a program that runs th test suite
    • Test result formatter: This formats the test results to produce the output in various human readable forms like plain text, HTML and XML
  • Refer Here where we have written the sample code to understand unittest Preview

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin