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

This is how i execute for simple modules. cd main/src python -m pytest py.test -v

Learning python object oriented. Please help me if my code is wrong (could be even in importing module as well). Actual question here is, can i execute python (containing class) modules along with pytest and option parser ?

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")

without pytest, i can run this as python test_custom_calculation.py --a 10 --b 10 --c add

how can i run this with pytest ?

EDITED :

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)
StackGuru
  • 471
  • 1
  • 9
  • 25

2 Answers2

0

Yes, Pytest has inbuilt parser option for the testcase.

Defined the below method in 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")

Kindly refer https://docs.pytest.org/en/latest/example/simple.html for more detail.

Use command:-- pytest -vsx test_custom_calculations.py --a= --o=

In test method,

test_custom_calculations.py

def test_cal(request):
    value_retrieved = request.config.getoption("--a")
nikhilesh_koshti
  • 393
  • 1
  • 10
  • Thanks Nikhilesh. Understood. How do i pass the arguments based on those options ? Can you give me an insight of conftest.py and passing arguments for above ? – StackGuru Oct 08 '18 at 08:27
  • and also, i have so many functions in test_custom_calculation.py and those each functions will get different arguments based on option parameter passed. How do i do this ? – StackGuru Oct 08 '18 at 08:35
  • Use command:-- {pytest -vs test_custom_calculations.py --o= --a=} – nikhilesh_koshti Oct 08 '18 at 08:43
  • Nikhilesh, where do you add intelligence of calling functions ? Say, 'test_custom_caluclation.py' has 5 functions (where i refer each of them as test-case). Each of these functions receives many different parameters. These arguments to be passed to each function depends upon which parser option has been passed. – StackGuru Oct 08 '18 at 08:48
  • For retrieving the value from command line , i have edited the above answers. – nikhilesh_koshti Oct 08 '18 at 08:49
  • For calling the functons, use the pytest request object because all the command line parameters can be accessible from request.config.getoption() – nikhilesh_koshti Oct 08 '18 at 08:52
  • say, if --o == option1, pass some arguments a1,b1,c1,d1 to fun1 (of test_calcuation.py). say, if --o == option2, pass some arguments a2,b2,c2,d2 to fun2 (of test_calcuation.py) – StackGuru Oct 08 '18 at 08:54
  • Refer use of paramterization in testcase for having different values:-- https://docs.pytest.org/en/latest/parametrize.html – nikhilesh_koshti Oct 08 '18 at 09:08
  • @StackGuru Kindly upvote or accept the answer if it resolve your problem. – nikhilesh_koshti Oct 08 '18 at 10:04
  • Nikhilesh, thanks for your valuable comments. I'm getting into it. I have edited my question above, hope that gives you more detailed view. please get me through it. – StackGuru Oct 08 '18 at 10:56
  • First of all, the parameters in test_* methods should be in fixture form, you can't pass an variable/parameter in test method in pytest. – nikhilesh_koshti Oct 08 '18 at 11:48
0

According to your question, i understood that you want to pass operations(add,sub) as a command line parameters, and execute the operations with the various val*.

So in Pytest, You can refer my answer:-- A way to add test specific params to each test using pytest

So it is based on test method name, logic should be handled in the fixture.

nikhilesh_koshti
  • 393
  • 1
  • 10
  • Great. So, how do we invoke the test here ? by respective classnames ? if that's the case don't we require option parser here ? – StackGuru Oct 09 '18 at 00:18
  • got it, this is the way how i invoke it pytest -s -v demo/test.py::TestClassA | Specific function : pytest -s -v demo/test.py::TestClassA::test_1 – StackGuru Oct 09 '18 at 03:41
  • now, if i need to pass some more parameters from command line (same like option parser, --name abc) and pass that value 'abc' to the function/class. how can i do it here ? – StackGuru Oct 09 '18 at 04:23
  • any idea ? for option-params as mentioned above – StackGuru Oct 09 '18 at 13:41
  • if you pass optional parameters using --name=adbc, you can get the value using request.config.getoption("--name"). – nikhilesh_koshti Oct 09 '18 at 18:25