如何执行 python class 模块以及 pytest 和选项解析器?

How to execute python class modules along with pytest and option parser?

main
  |--> src
         |--> custom_calculation.py
         |--> test_custom_calculation.py

custom_calculation.py

def calc_total(a,b):
    return a+b

def calc_multiply(a,b):
    return a*b

test_custom_calculation.py

import custom_calculation

def test_calc_sum():
    total = custom_calculation.calc_total(10,10)
    assert total == 20

def test_calc_multiply():
    result = custom_calculation.calc_multiply(10,10)
    assert result == 100

这就是我执行简单模块的方式。 CD main/src python -m 测试 py.test-v

学习python面向对象。如果我的代码错误(甚至可能在导入模块中),请帮助我。这里的实际问题是,我可以执行 python(包含 class)模块以及 pytest 和选项解析器吗?

main
  |--> A
         |--> custom_calculation.py
  |--> src
         |--> test_custom_calculation.py

test_custom_calculation.py
from optparse import OptionParser
from A import *
import sys

class Test_Custom_Calculation():

    def test_calc_sum():
        total = custom_calculation.calc_total(10,10)
        assert total == 20

    def test_calc_multiply():
        result = custom_calculation.calc_multiply(10,10)
        assert result == 100

if __name__ == "__main__":
    O = Test_Custom_Calculation()

    parser = OptionParser()

    parser.add_option("-a", "--a", dest="a", default=None,
                      help="Enter value of a")

    parser.add_option("-b", "--b", dest="b", default=None,
                      help="Enter value of b")

    parser.add_option("-o", "--o", dest="o", default=None,
                      help="specify operation to be performed")

    (options, args) = parser.parse_args()

    if options.a is None or options.b is None or options.c is None:
        sys.exit("provide values of a,b and specify operation")

    if options.c == "add":
        O.test_calc_sum(a,b)
    elif options.c == "mul":
        O.test_calc_multiply(a,b)
    else:
        sys.exit("Specify appropriate operation")

没有pytest,我可以运行这个python test_custom_calculation.py --a 10 --b 10 --c add

如何使用 pytest 运行 这个?

已编辑:

test_sample.py
def test_fun1(val1, val2, val3):

def test_fun2(val4,val5,val1):

def test_fun3(val6,val7,val8):

def test_fun4(val9,val10,val2):

def test_fun5(val2,val11,val10):

conftest.py
import pytest

def pytest_addoption(parser):

    parser.add_option("-a", "--add", dest="add", default=None,
                      help="specifies the addition operation")

    parser.add_option("-s", "--sub", dest="sub", default=None,
                      help="specifies the subtraction")

    parser.add_option("-m", "--mul", dest="mul", default=None,
                      help="specifies the multiplication")

    parser.add_option("-d", "--div", dest="div", default=None,
                      help="specifies the division")

    parser.add_option("-t", "--trigonometry", dest="trigonometry", default=None,
                      help="specifies the trigonometry operation")

where to define those functional arguments val* ?
where can we decide the logic of handling optional parser ?
    say, if option.add and option.sub:
            sys.exit("Please provide only one option")

        if option.add is None :
            sys.exit("No value provided")

        if option.add == "add":
            test_fun1(val1,val2,val3)

是的,Pytest 有内置的测试用例解析器选项。

在 conftest.py 中定义了以下方法。

def pytest_addoption(parser):
    """
    Command line options for the pytest tests in this module.
    :param parser: Parser used for method.
    :return: None
    """
    parser.addoption("--o",
                     default=None,
                     actions="store"
                     help="specify operation to be performed")

详情请参考https://docs.pytest.org/en/latest/example/simple.html

使用命令:-- pytest -vsx test_custom_calculations.py --a= --o=

在测试方法中,

test_custom_calculations.py

def test_cal(request):
    value_retrieved = request.config.getoption("--a")

根据你的问题,我了解到你想将操作(add,sub)作为命令行参数传递,并使用各种 val* 执行操作。

所以在 Pytest 中, 你可以参考我的回答:--

所以是基于测试方法名,逻辑应该在fixture中处理。