0% found this document useful (0 votes)
49 views4 pages

Assert and Exceptions Presentation

- Pytest allows the use of the built-in Python assert statement for performing verifications in unit tests and provides more context in failure messages. It also allows validating floating point values using approx and verifying exceptions are raised using the raises helper.

Uploaded by

RAMESH SIVA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

Assert and Exceptions Presentation

- Pytest allows the use of the built-in Python assert statement for performing verifications in unit tests and provides more context in failure messages. It also allows validating floating point values using approx and verifying exceptions are raised using the raises helper.

Uploaded by

RAMESH SIVA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Using Assert and Testing

Exceptions

In this lecture I’m going to go over using the built-in Python assert statement in your unit tests and how to test exceptions in Pytest.
Using the assert Statement
def test_IntAssert(): • Pytest allows the use of the built in
assert 1 == 1 python assert statement for
def test_StrAssert(): performing verifications in a unit test.
assert “str” == “str” • Comparison on all of the python data
def test_floatAssert(): types can be performed using the
assert 1.0 == 1.0 standard comparison operators: <, >,
def test_arrayAssert(): <=, >=, ==, and !=
assert [1,2,3] == [1,2,3] • Pytest expands on the message
returned from assert failures to
def test_dictAssert():
provide more context in the test
assert {“1”:1} == {“1”:1} results.

- Pytest allows the use of the built in python assert statement for performing verifications in a unit test.

- The normal comparison operators can be used on all python data types: less than, greater than, less than or equal, greater than or equal, equal, or not equal

- Pytest expands on the messages that are reported for assert failures to provide more context in the test results.
Comparing Floating Point Values
• Validating floating point values can
sometimes be difficult as internally
# Failing Test!!!
the value is a binary fractions (i.e. 1/3
def test_BadFloatCompare(): is internally 0.33333333…)
assert (0.1 + 0.2) == 0.3
• Because of this some floating point
comparisons that would be expected
# Passing Test!!! to pass fail.
def test_GoodFloatCompare(): • The pytest “approx” function can be
val = 0.1 + 0.2 used to verify that two floating point
values are “approximately” equivalent
assert val == approx(0.3)
to each other with a default tolerance
of 1e-6.

- Validating floating point values can sometimes be difficult as internally the value is stored as a series of binary fractions.

- Because of this some comparisons that we’d expect to pass will fail.

- Pytest provides the “approx” function which will validate that two floating point values are “approximately” the same value as each other to within a default tolerance of
1 time E to the -6 value.
Verifying Exceptions
• In some cases we want verify that a
def test_Exception(): function throws an exception under
with raises(ValueError) certain conditions.
raise ValueError
• Pytest provides the “raises” helper to
perform this verification using the
“with” keyword.
• If the specified exception is not
raised in the code block specified
after the “raises” line then the test
fails.

- In some test cases we need to verify that a function raises an exception under certain conditions.

- Pytest provides the raises helper to perform this verification using the “with” keyword.

- When the “raises” helper is used the unit test will fail if the specified exception is not thrown in the code block after the “raises line.

You might also like