Giter VIP home page Giter VIP logo

unittestspractice's Introduction

Python Unit Tests Practice with pytest and coverage plugin

Tests Tests Tests

Repo is based on the simple order payment processor which checks credit card credentials e.g. card number via Luhn algorithm and card validity (expiration date). Running the script CLI asks user for credit card information and charges the card with static Line Items defined in main.py

Additionally github Actions (workflows) were tested with the use of the repository secret variable API_KEY.

Main point of this repo was to learn how to write basic and more advanced unit tests with pytest package. After that it was easier to notice where refactoring will simplify the code and our unit tests as a result. More difficult tests were done with the use of MonkeyPatch to override the built-in OS input reading system. Other thing was creating mock function to replace current charge method so that running the tests won’t charge provided credit card. Coverage plugin for pytest was helpful to check which part of the code still needs to be included in the unit tests.

Main points of the refactored code:

  • Used dependency injection so that the objects that are needed are provided as a arguments instead of being created by the class itself.
  • Dependency inversion allows to replace payment processor with other one without the need to change the code in previous function.
  • Created payment processor mock class.
  • Removed input patching.
  • Used a fixture for the credit card and payment processor object so that they can be easily supplied to the unit tests without the repetition of the code.
  • Removed hardcoded dates from the unit tests so that the tests will be always passing.
  • Turned Luhn checksum into a function cause it didn’t use any of the class variables.
  • Hidden API_KEY with .env file and used python-dotenv package to load environmental variable into the script.

Source: https://github.com/ArjanCodes/2022-test-existing-code

unittestspractice's People

Contributors

bunnicz 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.