A
A
Alexander2020-07-24 21:45:27
Python
Alexander, 2020-07-24 21:45:27

How to effectively perform the same type of tests?

A database is written as a project in X language.
For functional tests, we chose Python and Pytest.
You need to write tests for errors that the base should handle - for example, if you run a query like "insert ... 1/0", the base should return an error and code.

I wrote some tests like

def test_zero_division(cursor_fixture):
    cur = cursor_fixture
    with pytest.raises(<ExceptionClassName>) as e:
        cur.execute(query)
    assert e.value.pgcode == some_error_code


But there are a lot of such errors in the 12.5 version of Postgres and I want to test them somehow and not write a separate test for each. There are options ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
javedimka, 2020-07-24
@aleks0010

@pytest.mark.parametrize('err, exc', [(1122, ExceptionClass1), (2233, ExceptionClass2)])
def test_zero_division(cursor_fixture, err, exc):
    cur = cursor_fixture
    with pytest.raises(exc) as e:
        cur.execute(query)
    assert e.value.pgcode == err

How did you choose that?
https://docs.pytest.org/en/stable/parametrize.html
By the way, this test makes exactly 0 sense.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question