How to do a class or test function with pytest?

Asked

Viewed 483 times

0

Be a Python program, for example, the calculation of Fibonnaci:

def fibR(n):
 if n==1 or n==2:
  return 1
 return fib(n-1)+fib(n-2)
print (fibR(5))

How to do a test class/function using pytest, for example?

1 answer

0


def fibR(n):
 if n==1 or n==2:
  return 1
 return fibR(n-1)+fibR(n-2)

In the same directory:test_fib.py:

import pytest
from fib import fibR

def test_fib_1_equals_1():
    assert fibR(1) == 1

def test_fib_2_equals_1():
    assert fibR(2) == 1

def test_fib_6_equals_8():
    assert fibR(6) == 8

Reply from @Tagc of the English version of stackoverflow.

  • 2

    Nice of you to ask and answer. I do that, too. I just think you should add the answer link and explain, for example, that the functions should start with a "test_" prefix. I think the answer is important not to have just code. See you later!

  • @Wallace Maxters, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.