Skip to main content

Delayed/soft assertions for python

Project description

Build Status PyPI version Downloads Downloads

Python-Delayed-Assert

Delayed aka. Soft asserts for python

Few features:

- No Dependenices on any other framework/library.
- Should work with any testing framework.
- Can be use as decorator or context manager.

Sample

Installation

Install via pip

    pip install delayed-assert

Install from master

    pip install git+https://github.com/pr4bh4sh/delayed-assert

Uses

See example_unittest.py for usage.

Using assertion library with lambda

Pass the assertion call as

    expect(lambda: self.assertListEqual([4,5,6,2,5],[7,8]))

While I've tested only with unittest asserttion,It should be able to use any assertion library.

Keep in mind that, Python does not support statement inside lambda, so

    expect(lambda: assert 1 == 1)

won't work as it is not a valid lambda expression in python

Current possible uses

    def testSomething(self):
        delayed_assert.expect(1 == 1) # will succeed
        delayed_assert.expect(1 == 2) # will fail but won't stop execution
        delayed_assert.expect(3 == 2, "Value don't match") # will fail but won't stop execution
        delayed_assert.expect(3 == 3) # will succeed
        # will stop execution and show the stack trace of 2nd assertion
        delayed_assert.assert_expectations()

    def testLambdas(self):
        expect(lambda: self.assertEqual(3,4)) # will fail but won't stop execution
        expect(lambda: self.assertListEqual([4,5,6,2,5],[7,8])) # will fail but won't stop execution
        assert_expectations()

    @delayed_assert.assert_all()
    def testDecorator(self):
        expect('five' == 'Six', 'String do not match')
        expect([5,2] == [3,4], 'List item do not match')
        expect([3,4] == [3,4], 'This message wont be printed')
        # No need to call delayed_assert.assert_expectations() when decorator is used

    def testContextManeger(self):
        with delayed_assert.assert_all():
            expect('four' == 'Six', 'String do not match')
            expect([5,2] == [3,4], 'List item do not match')
            expect([3,4] == [3,4], 'This message wont be printed')
            # No need to call delayed_assert.assert_expectations() when using context maneger is used

Credit : http://pythontesting.net/strategy/delayed-assert/

Project details


Download files

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

Files for delayed-assert, version 0.3.6
Filename, size File type Python version Upload date Hashes
Filename, size delayed_assert-0.3.6-py3-none-any.whl (4.7 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size delayed_assert-0.3.6.tar.gz (4.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page