Post

Python Pytest examples

@pytest.mark.parametrize example

Used to pass more than one case to a test.

The next example is to test a funcion that validates the string conforms to the Juniper naming convention. We expect the function to return a boolean.

We test the function by passing many differet inputs (where not all of them are strings) along with the expected boolean value we expect the function to return on each case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import pytest

def is_valid_juniper_interface_format(interface) -> bool:
    """
    Function that validates that we are passing as a string a Juniper interface
    that adheres to the Junos OS naming convention
    """
    ...
    return result

@pytest.mark.parametrize("interface, result", [
    ('ge-0/0/1', True),
    ('ge-0/1/0', True),
    ('ge-1/1/0', True),
    ('xe-0/0/2', True),
    ('et-0/0/3', True),
    ('xe-0/0/1:0', True),
    ('et-0/0/0:1', True),
    ('et-0/0/0:2', True),
    ('et-0/0/0:3', True),
    ('et-0/0/0:4', True),
    ('ge-0/0/12xy', False),
    ('xxe-0/0/7', False),
    ('eth1/2', False),
    ('FastEthernet0/0', False),
    ('GigabitEthernet0/1', False),
    ('', False),
    ('1234', False),
    (1234, False),
    ([], False),
    ((), False),
    ({}, False),
    (True, False),
    (False, False),
])
def test_juniper_interface_format(interface, result):
    """
    Test to validate that we are passing as a string a Juniper interface
    that adheres to the Junos OS naming convention
    """
    assert is_valid_juniper_interface_format(interface) == result

References

This post is licensed under CC BY 4.0 by the author.