I'm wondering if there's a way to define step test functions inside Python class with pytest-bdd
framework.
Let me elaborate a bit further..., suppose that we have a feature file like this:
"test.feature" file:
Feature: Transaction
Calculate the leftover money at the end of a transaction
Scenario Outline: Withdraw Money
Given I deposit <initial_amount>
When I withdraw <withdrawn_amount>
Then I should only have <leftover_money> left
Examples:
| initial_amount | withdrawn_amount | leftover_money |
| $100 | $30 | $70 |
| $1000 | $50 | $950 |
| $3000 | $200 | $2800 |
and then we have the step functions written like this:
"test_transaction.py" file:
from pytest_bdd import scenario, given, when, then, parsers
import pytest
def pytest_configure():
pytest.AMT = 0
class TestWithdrawal:
@scenario('test.feature', 'Withdraw Money')
def test_withdraw():
pass
@given(
parsers.cfparse(
"I deposit ${amount:Number}",
extra_types={"Number": int}
),
target_fixture="initial_amount"
)
def initial_amount(amount):
return amount
@when(
parsers.cfparse(
"I withdraw ${withdrawn_amount:Number}",
extra_types={"Number": int}
)
)
def withdraw_money(initial_amount, withdrawn_amount):
pytest.AMT = initial_amount - withdrawn_amount
@then(
parsers.cfparse(
"I should only have ${leftover_amount:Number} left",
extra_types={"Number": int}
)
)
def leftover_money(leftover_amount):
assert pytest.AMT == leftover_amount
When I tried to run the test with this command from my terminal:
pytest test_transaction.py
I get this error below:
=================================================================================================== ERRORS ====================================================================================================
_______________________________________________________________________________ ERROR collecting test_transaction_with_class.py _______________________________________________________________________________
invalid method signature
During handling of the above exception, another exception occurred:
Could not determine arguments of <bound method step.<locals>.decorator.<locals>.step_function_marker of <test_transaction_with_class.TestWithdrawal object at 0x1071aa550>>: invalid method signature
=========================================================================================== short test summary info ===========================================================================================
ERROR test_transaction_with_class.py::TestWithdrawal
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================== 1 error in 0.06s ===============================================================================================
which I think means that pytest-bdd
framework doesn't support grouping a set of tests inside a class.
if this is true..., this is almost a deal breaker for me as we can easily group a set of tests inside a class with pytest
.