Skip to main content

A small library for testing your code, made by tester for testers

Project description

README

A simple library to test your python code.

Key Features:

  • no third-party dependencies, only the standard library is used
  • no need to inherit from any classes
  • no need to name your files and/or tests with the prefix or postfix 'test' added
  • it is possible to use native python assert as well as library asserts
  • simple and understandable work with tests, data providers, checks
  • the ability to run based on a file with settings or passing arguments on the command line
  • automatic search for all tests in the current folder and subfolders
  • flexible configuration of both tests and their groups, the ability to group tests and run only selected groups
  • the ability to use both the built-in results processing tool and write your own
  • the ability to group, stop the test by timeout, parallel launch, mocking without installing additional plugins

Installation

Just use your pip

pip install checking

First test

Simple example:

from checking import *

def my_function_to_test(a,b)
    return a + b

@test
def any_name_you_like():
    # Check  1+1=2
    equals(2, my_function_to_test(1,1))

if __name__ == '__main__':
    # Runs all tests in current module
    start()

Only functions marked with the @test annotation are considered tests and will be run, you can name your tests as you wish, the main thing is to put the @test annotation

Basic Asserts

You can use simple python assert if you like, but it recommended to use simple and readable library asserts.

Standard checks:
#!python
@test
def checks_basic_asserts():
    is_true('1', 'Error message') # checks value is True
    is_false([], 'Error message') # checks value is False
    equals(1, 1, 'Error message') # checks two objects are equal (==)
    not_equals(1, 2, 'Error message') # checks two objects are equal (!=)
    is_none(None, 'Error message') # checks object is None
    not_none('1', 'Error message') # checks object is not None
    contains(1, [1, 2, 3], 'Error message') # checks that second argement contains first arg
    not_contains(4, [1, 2, 3], 'Error message') # checks that second argement not contains first arg


Messages in all asserts are optional, but it strongly recommended to use them!

Work with or without exceptions

If you need to check the exception raises and its message you can write test like:

#!python
@test
def check_with_exception():
    with waiting_exception(ZeroDivisionError) as e:
        x = 1 / 0 # Here exception will be raised!
    assert e.message == 'division by zero' # Check message (using python assert)

If no exception will be raised or it will be exception of another type - test will fail. Pay attention - you must check message (if you need to) after exiting context manager, but not inside its scope!

You can't use BaseException here, and it strongly recommended not to use Exception as parent of all exceptions!

In some cases, you nedd just to run some code and make sure no exception raised. There is a special way for that:

#!python
@test
def no_exceptions_bad():
    do_something() # Bad way! No asserts here, is that a test???

@test
def check_no_exception_raises():
    with no_exception_expected(): # Explicitly say that we are not waiting exceptions
        do_something() # Good way!


If any exception will be raised - test will fail

Managing test during execution

Sometime you need to fail or brake test in execution time on some reason (wrong OS, parameters, etc.)

#!python
@test
def must_fail_on_condition():
    if some_condition():
        test_fail('Expected fail!')


@test
def must_be_broken():
    if some_condition():
        test_brake('Expected brake!')

Soft and Fluent Asserts

Soft Assert

Soft Assert is a convenient way to checks a few condition before fail. Standard test is preferably fail fast, and if some check fails - the test stops. But sometime you need to check a list of conditions, and check them to fail only at the end of the test, with all information what checks were failed. For example you have a json object and want to checks all its fields, but don't want to stop test at first failed check, because you want to know what about all other fields!

#!python

@test
def check_all_json_fields():
    my_json = get_my_json_somethere()

    soft_assert = SoftAssert() # Creates an object of soft assert
    soft_assert.check(lambda : equals(1, my_json['field1'], 'message')) # Check field, test will not fail here!
    soft_assert.check(lambda : equals('text', my_json['field2'], 'message'))
    soft_assert.contains(1,my_json['list'])
    soft_assert.assert_all() # If something wrong, test will fail here!

Attention! You always must use assert_all() at the end of the test, only at that moment all exception (if something wrong) will be raise.

Fluent Assert

Fluent assert is just a sugar to make chains of checks for the object, they are simple, readable, but it is NOT a soft asserts! If one of the checks will fail - test stops! Fluent asserts have analogues of the basic asserts, but also have their own types, you can fimd them all at checking/classes/fluent_asserts.py

#!python

@test
def check_list_valid():
    my_list = get_my_list_somethere()

    verify(my_list).is_not_none().AND.is_a(list).AND.contains(2).AND.is_sorted()

Contact me

Lexman2@yandex.ru

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

checking-0.1.8.tar.gz (32.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

checking-0.1.8-py3-none-any.whl (40.0 kB view details)

Uploaded Python 3

File details

Details for the file checking-0.1.8.tar.gz.

File metadata

  • Download URL: checking-0.1.8.tar.gz
  • Upload date:
  • Size: 32.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for checking-0.1.8.tar.gz
Algorithm Hash digest
SHA256 a5b72184774a1750dcd3ed293aa5ba120ea004662979a21006c366b4f231f721
MD5 1da6143d9a6b28e6470d4f3ab6dafabf
BLAKE2b-256 49c767ca15e8be0e579f7ab8bf423d2716e148effb16208804bd06ac86f8e233

See more details on using hashes here.

File details

Details for the file checking-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: checking-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 40.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for checking-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 e2aa006be082812d9d72205bee071f28b09fdedd3b8835340b717526e01e1247
MD5 85385511b0efca1b00a35b235cc1396c
BLAKE2b-256 35c79f63669c65e715ff85a2061782fc18de3bb532df48a6307685b6485a9f1f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page