Search code examples
pythonbddpytest

Test files and features files to be in different directories


How to make test files and feature files in different directories in py-test-bdd?

I have had a look at the py-test documentation under "Organizing your scenarios" section.

My current structure is:

bdd
  |
  feature directory
                  |
                  feature file #1 (named log.feature)
                  feature file #2
                  feature file #3      
  |
  test file #1
  test file #2
  test file #3

with current code doing:

@scenario('features/log.feature', 'scenario description #1')

But instead what I would actually like is:

bdd
  |
  feature directory
                  feature file #1
                  feature file #2
                  feature file #3      
  |
  test directory (named tests)
                test file #1
                test file #2
                test file #3

I tried writing the code:

@scenario('bdd/features/log.feature', 'scenario description #1')

But when executing this line above what is actually tries to do is bdd/tests/bdd/features/log.feature and obviously it throws an error directory does not exist.
How do I get it to do bdd/features/log.feature ?


Solution

  • Just need to do ../ to jump to preceding directory. ie:

    @scenario('../features/log.feature', 'scenario title #1')
    

    If you have more directories, just need to do ../../../ and repeat ../ as many times as directories as you want to go upwards.

    If you have more than 1 scenario, then just declare a variable at the top and pass in this variable to every @scenario, ie:

    myFilePath = '../features/log.feature'
    @scenario(myFilePath, 'scenario title #1')
    @given('blah....')
    @then('blah....')
    
    @scenario(myFilePath, 'scenario title #2')
    @blah