Git Product home page Git Product logo

python_unit_testing's Introduction

python_unit_testing

a class about unit testing in python

  1. why is unit testing?
  2. methods of doing it in python with examples

1) why is unit testing?

many reasons...

  • a more structured way of thinking... in a function where you need to do several actions, you can create little functions for each of the actions and test them as you go individually, so that you will be aware of bugs faster... OR, create first the tests and then the little functions (TDD - test driven development), which usually makes you code more rapidly.

  • when working on new features to implement on an existing codebase, we want to make sure that after our change/refactoring, everything still works.

  • the new code will only be in production for users, if the tests pass! so it makes it easier for you and your team to be sure of their changes... it limits anxiety when going to bed at night.

  • it's not just for fun, it's crucial sometimes, for example a bank, a hospital, military, ...

however...

  • because we are only testing things individually, we don't know if they work as supposed to all together... that is where integration and e2e tests come in! (https://i.imgur.com/w1uYhFA.png)

  • it's horribly boring to do in my opinion, it takes a lot of time and effort.

  • also doesn't cover performance or scalibility.

references

https://www.geeksforgeeks.org/unit-testing-software-testing/ (also explains differences between the different types of testing)

2) methods of doing it in python with examples

the assert statement is the basis of testing; it throws an exception when it's false:

assert <condition being tested>, <error message to be displayed>

assert 1 > 0, 'The Condition is True'

assert 1 < 0, 'The Condition is False'

on top of it there are many frameworks that make our lives easier...

  • pytest
  • unittest (built-in in python)
  • doctest
  • nose2 ...

example with pytest and unittest: https://github.com/Mrrvm/python_unit_testing

pytest

install pytest: pip install pytest

run all tests: python -m pytest tests/test_fruit_pytest.py

run test_request inside logic class: python -m pytest fruit.py

  • can be next to the logic, which might be more or less organised
  • only uses assert

unittest(built-in in python)

run all tests verbosely: python -m unittest -v

  • must be in separate file, in a folder called tests, have an init file and call unittest.main()
  • has many assert methods: assertEqual, asserNotEqual, assertIn, assertInstance, assertIs, ...

references

about the different frameworks https://www.aviator.co/blog/complete-guide-to-python-testing-frameworks/

unittest https://www.dataquest.io/blog/unit-tests-python/https://docs.python.org/3/library/unittest.html (official docs)

pytest vs unittest https://machinelearningmastery.com/a-gentle-introduction-to-unit-testing-in-python/

python_unit_testing's People

Contributors

mrrvm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.