Code Monkey home page Code Monkey logo

python-unit-tests's Introduction

Python Unit Testing Example

This is a simple example of how to use Python's built-in unittest module to write and run unit tests.

Interleave string solution is from my own Leetcode submission.

Quick Start

  • To run the tests and downloaded required packages:
sh build.sh
  • Example of Canary and regular tests are in ./tests/interleave_tests.py
  • Expected result is
Ran 5 tests in 0.000s

OK

Structure

  • build.sh: This script installs required pip packages and runs the tests using "python3."
  • requirements.txt: This file is used by pip to install packages. If you require any packages, you can add them here.
  • src: This directory contains your source files, i.e., your code.
  • tests: All your tests are located here.

Testing

unittest is the preinstalled Python testing library. There are alternatives you may consider, such as Pytest. Running the test is as simple as:

python -m unittest [directory]

If you prefer a more verbose output, add -v:

python -m unittest -v [directory]

Packages

For most, pip is your go-to for packages. For those coming from JavaScript, there isn't a package.json; the alternative is requirements.txt. You can include any required packages in this file, and anyone using pip can install them simply.

Example requirements.txt:

pip==22.3.1
wheel==0.38.4
Pillow==9.5.0
setuptools==65.5.1
packaging==23.1
numpy==1.26.0

To install the packages, it is as simple as:

pip install -r requirements.txt

Have fun! If you find a mistake or an improvement, please drop a pull request and let me know!

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.