简体   繁体   中英

Loop executing n-1 times in python-behave

I have a step in behave that calls a method in another python class as follows

@when('we query for a specific resource')
def step_impl(context):

context.resource='myresource'
jsonutil=JSONUtil.JSONUtil()
response=jsonutil.parseJSON(context.resource)
assert True

The parsejson method is as follows in JSONUtil class

def parseJSON(self,resource):

    url= "http://"+data.test.engine_url+'/api/v1/policies?resource='+resource
    response=requests.get(url)
    time.sleep(5)
    json_data = json.loads(response.text)
    #time.sleep(5)
    #print(json_data)
    x=[1,2,3]
    for i in x:
        print("%d" % i)

    #for json_obj in json_data:
    #        if 'statements' in json_obj:
    #            print(json_obj['statements'][0]['resource'][0])

    return response

When this step is executed. I get the following output

1
2
<<Note that 3 does not get printed>>

But when the method parsejson is invoked in the following manner

J=JSONUtil()
J.parseJSON('myJSON')

I get the following output

1
2
3

Any reason/pointers as to why the code when invoked using behave executes the for loop n-1 times?

There is only thing I know of that would explain why code that properly produces the expected lines outside Behave suddenly produces an output that lacks the last line if run in Behave: the output is getting overwritten with Behave's output. By default just before Behave runs a step, it prints out to the screen the step's name in a neutral color, after the step is run Behave overwrites last line of output with the step's name in a color indicating whether the step failed, succeeded, is undefined, etc.

My remedy is to just add more newlines in my printouts. You can also use --no-color . I prefer having colors so I add newlines.

Here's an illustration. Consider this feature file:

Feature: foo

Scenario: foo
  When something
  And something else
  And and another thing

And these steps:

@when("something")
def step_impl(context):
    for i in (1, 2, 3):
        print("%d" % i)

@when("something else")
def step_impl(context):
    for i in (1, 2, 3):
        print("%d" % i)
    print()

@when("and another thing")
def step_impl(context):
    pass

If you run behave --no-capture :

behave --no-capture
Feature: foo # features/foo.feature:1

  Scenario: foo           # features/foo.feature:3
    When something        # features/steps/steps.py:1
1
2
    When something        # features/steps/steps.py:1 0.000s
    And something else    # features/steps/steps.py:6
1
2
3
    And something else    # features/steps/steps.py:6 0.000s
    And and another thing # features/steps/steps.py:12 0.000s

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

(The output above would be colored but copy-and-paste does not preserve the colors.)

The step "When something" lacks the last line of output whereas "When something else" has all expected lines, only because it has an extra print at the end. However, note how for both step, the step name appears twice in the output. Accounting for colors, in both cases the first time the step name appears, it is grey, and the second time, it is green. If you run it with --no-color too, you get:

Feature: foo # features/foo.feature:1

  Scenario: foo           # features/foo.feature:3
    When something        # features/steps/steps.py:1
1
2
3
    And something else    # features/steps/steps.py:6
1
2
3

    And and another thing # features/steps/steps.py:12

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

Both loops output the expected lines and the steps' names do not appear more than once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM