使用pytest控制函數運行的函數 需要使用
(1)使用 @pytest.mark.run(order=x) 標記被測試函數
(2)運行的順序由order傳入的參數決定;(order從小到大的順序執行)
import pytest class Calc(object): @classmethod def add(cls, x, y, *d): # 加法計算 result = x + y for i in d: result += i return result @classmethod def sub(cls, x, y, *d): # 減法計算 result = x - y for i in d: result -= i return result @classmethod def mul(cls, x, y, *d): # 乘法計算 result = x * y for i in d: result *= i return result @staticmethod def div(x, y, *d): # 除法計算 if y != 0: result = x / y else: return -1 for i in d: if i != 0: result /= i else: return -1 return result @pytest.mark.run(order=2) def test_add(): assert Calc.add(1, 2, 3) == 6 @pytest.mark.run(order=3) def test_add2(): assert Calc.add(1, 2, 3) == 6 @pytest.mark.run(order=1) def test_sub(): assert Calc.sub(100, 20, 30) == 50