0
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 pytest py.test -v

学习python面向对象。如果我的代码有误,请帮助我(甚至可以在导入模块中)。这里的实际问题是,我可以执行 python(包含类)模块以及 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)
4

2 回答 2

0

是的,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")
于 2018-10-08T06:44:34.480 回答
0

根据您的问题,我了解您希望将操作(添加,子)作为命令行参数传递,并使用各种 val* 执行操作。

所以在 Pytest 中,您可以参考我的答案:--一种使用 pytest 向每个测试添加测试特定参数的方法

所以它基于测试方法名称,逻辑应该在夹具中处理。

于 2018-10-08T11:54:52.427 回答