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