Python & Radish BDD

April 12, 2018 · Posted in Requests for info . . . · Comment 

I’m currently trying to get Radish BDD working but am struggling with the basics…

I’ve gone through the Getting Started stuff here and that all works fine.

The next step was to work through the Quickstart. It’s broadly similar to Getting Started, but rather than the functionality being inside the test it should import a class and test that…and that’s where I’m stuck. I keep getting the following error,

Error: Unable to import module ‘terrain’ from ‘radish/terrain.py’: No module named ‘calculator’

It’s basically the same as this issue on Stack Overflow, but the fix listed there isn’t working for me.

My files are laid out as follows

.
├── features
│ └── SumNumbers.feature
├── radish
│ ├── __init__.py
│ └── steps.py
│ └── terrain.py
└── calculator
  ├── __init__.py
  └── main.py

The contents of the files are below. I’m using Python 3.6 and Radish 0.8.3 and running the tests from the . directory using the followingcommand,

radish features/

That should let anyone else replicate what I’m doing. Can you make this work and if so how…what have I missed?


SumNumbers.feature

Feature: The calculator should be able to sum numbers
    In order to make sure the calculator
    sums numbers correctly I have the following
    test scenarios:

    Scenario: Test my calculator
        Given I have the numbers 5 and 6
        When I sum them
        Then I expect the result to be 11

steps.py

 # -*- coding: utf-8 -*-

from radish import given, when, then

@given("I have the numbers {number1:g} and {number2:g}")
def have_numbers(step, number1, number2):
    step.context.number1 = number1
    step.context.number2 = number2

@when("I sum them")
def sum_numbers(step):
    step.context.result = step.context.calculator.add( \
       step.context.number1, step.context.number2)

@then("I expect the result to be {result:g}")
def expect_result(step, result):
    assert step.context.result == result

terrain.py

# -*- coding: utf-8 -*-

from radish import before, after

from calculator.main import Calculator

@before.each_scenario
def init_calculator(scenario):
    scenario.context.calculator = Calculator(caching=True)

@after.each_scenario
def destory_calculator(scenario):
    del scenario.context.calculator

main.py

class Calculator(object):

    def add(self, a, b):
        return a + b

    def sub(self, x,y):
        return a - b

    def multiply(self, x,y):
        return a * b

    def divide(self, x,y):
        return a / b