Python > Testing in Python > pytest > Writing Simple Tests with pytest

Testing with Assertions for Different Data Types

This example shows how to write pytest tests using different data types and operators. It includes testing strings, lists, and boolean values.

Testing Different Data Types

This code defines four test functions using different data types. The test_string_equality function checks if two strings are equal. The test_list_equality function checks if two lists are equal. The test_boolean_true and test_boolean_false functions check if boolean values are True or False, respectively. The not operator inverts the boolean value.

def test_string_equality():
    assert 'hello' == 'hello'

def test_list_equality():
    assert [1, 2, 3] == [1, 2, 3]

def test_boolean_true():
    assert True

def test_boolean_false():
    assert not False

Concepts Behind the Snippet

The key concept is demonstrating the versatility of the assert statement with different Python data types. It highlights that you can use the same assertion logic to compare strings, lists, booleans, numbers, or any custom object that supports equality comparisons. This flexibility makes pytest a powerful tool for testing various aspects of your code.

Real-Life Use Case

Consider a function that processes user input and returns a list of validated values. You can use test_list_equality to verify that the function returns the expected list for a given input. Similarly, you can use test_boolean_true and test_boolean_false to check if a function returns the correct boolean flag based on certain conditions, e.g., indicating success or failure of an operation.

Best Practices

  • Test for both positive and negative cases: For example, test that a function returns True under normal conditions and False under error conditions.
  • Use descriptive error messages: While pytest provides default error messages, you can add custom messages to make it easier to understand why a test failed (e.g., assert x == y, 'Custom error message').
  • Consider edge cases: Always think about boundary conditions or unusual inputs that might cause unexpected behavior.

Interview Tip

When discussing testing, mention that effective testing involves covering different code paths and handling various data types appropriately. You can highlight that you understand how to design tests that account for different scenarios, improving code reliability and robustness.

When to use them

Use these data type specific tests when verifying functions that manipulate strings, lists, or any data type. They provide precise verification when correctness depends on these datatypes

Alternatives

Alternatives to direct equality checks (==) for lists include using pytest.approx() for approximate equality of floating-point numbers, or using set operations (e.g., issubset, issuperset) for unordered collections. You can also use more specialized assertion methods provided by pytest plugins (e.g., for comparing dictionaries).

FAQ

  • Can I use other comparison operators besides ==?

    Yes, you can use any comparison operator (e.g., !=, >, <, >=, <=) in your assertions.
  • How do I compare floating-point numbers accurately?

    Use pytest.approx() to account for floating-point precision issues (e.g., assert 0.1 + 0.2 == pytest.approx(0.3)).