Testing Depth First Search Using Pytest I have an implementation of depth first search, which I want to test using the pytest framework.
I would be making test cases for other graph algorithms, and would like to know if this approach is good enough to be replicated on similar programs.
Here is what it looks like currently:
(This may not be a very exhaustive set of test cases, but my goal is to separate simple checks that are done in the actual implementation)
import pytest
from dfs import dfs_recursive, dfs_iterative


def test_data():
    test_graph = {
        'A' : ['B','S'],
        'B' : ['A'],
        'C' : ['D','E','F','S'],
        'D' : ['C'],
        'E' : ['C','H'],
        'F' : ['C','G'],
        'G' : ['F','S'],
        'H' : ['E','G'],
        'S' : ['A','C','G']
    }

    assert dfs_iterative(test_graph, 'A') is not None
    assert len(dfs_iterative(test_graph, 'A')) == len(list(test_graph.keys()))
    assert dfs_recursive(test_graph, 'A') is not None
    assert len(dfs_recursive(test_graph, 'A')) == len(list(test_graph.keys()))


def test_graph():
    test_graph = {
        1: [2, 3]
    }

    if len(test_graph.keys()) &lt; 2:
        print(&quot;\nA graph has to have atleast 2 vertices&quot;)


def all_unique(x):
    &quot;&quot;&quot;
    Check if all elements in a list are unique; if not exit erly
    &quot;&quot;&quot;
    seen = set()
    return not any(i in seen or seen.add(i) for i in x)

def test_unique():
    test_graph = {
        'A': ['B', 'C'],
        'B': ['A', 'D', 'E'],
        'C': ['A', 'F'],
        'D': ['B'],
        'E': ['B', 'F'],
        'F': ['C', 'E']
    }

    assert all_unique(dfs_iterative(test_graph, 'A')) is True
    assert all_unique(dfs_recursive(test_graph, 'A')) is True


def test_vertex1():
    test_graph = {
        'A': ['B', 'C'],
        'B': ['A', 'D', 'E'],
        'C': ['A', 'F'],
        'D': ['B'],
        'E': ['B', 'F'],
        'F': ['C', 'E']
    }

    with pytest.raises(KeyError) as excinfo:
        dfs_iterative(test_graph, 'Z')

    assert 'Z' in str(excinfo.value)
    assert 'KeyError' in str(excinfo.type)
    print('\nVertex does not exist')


def test_vertex2():
    test_graph = {
        1: [2, 3],
        2: [2, 3, 4],
        3: [],
        4: [],
    }

    for key, value in test_graph.items():
        if key in value:
            pytest.fail(&quot;A vertex cannot point to itself&quot;)    # explicitly fails this test


Please suggest any changes, additions or improvements.
Three observations:

There are two many assert statements in a single test function. Ideally each function should only test one thing. That way when the test fails you know exactly what is wrong.
Instead of initializing test data in the test function, you can write fixtures. Each fixture can be used in multiple test functions.
You don't need to assert 'KeyError' in str(excinfo.type) since you already has with pytest.raises(KeyError).