python-斐波拉

test_fib.py

import pytest


def fib():
    """1-n的fib数列生成器"""
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b


class Fib:
    """Fib类"""

    def __init__(self):
        self._a = 0
        self._b = 1

    def __iter__(self):
        return self

    def __next__(self):
        _ = self._a
        self._a, self._b = self._b, self._a + self._b
        return _


def get_fib_nums(fib, n):
    fib_nums = []
    for i in fib:
        if i > n:
            break
        fib_nums.append(i)
    return fib_nums



@pytest.mark.parametrize('fib,n', [(fib(), 100), (Fib(), 100)])
def test_case(fib, n):
    fib_nums = get_fib_nums(fib, n)
    print(fib_nums)


console

collecting ... collected 2 items

test_fib.py::test_case[fib-100] 
test_fib.py::test_case[fib1-100] 

============================== 2 passed in 0.17s ==============================

Process finished with exit code 0
PASSED                                   [ 50%][0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
PASSED                                  [100%][0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

posted @ 2021-08-28 19:27  花兒向陽開  阅读(46)  评论(0编辑  收藏  举报