Saturday, September 14, 2024

Pytest

 

pipenv pip install pytest


Now, I have a simple function in a file

t.py

def square(x: float):

return x * x


t_test.py

import t

def test_square():

assert t.square(5) == 25


Now, enhancing the test case code for running tests for multiple cases.


t_test.py

import t

import pytest

@pytest.mark.parametrize(

('input_n', 'expected'),

(

(5,25),

(3.,9.),

)

)


def test_square(input_n, expected):

assert t.square(input_n) == expected


Now, adding a class,


t_test.py

import t

import pytest


@pytest.mark.parametrize(

('input_n', 'expected'),

(

(5,25),

(3.,9.),

)

)

def test_square(input_n, expected):

assert t.square(input_n) == expected


class TestSquare:

def test_square(self):

assert t.square(3) == 9


No comments:

Post a Comment