Skip to content

Conversation

@lonvia
Copy link
Member

@lonvia lonvia commented Oct 30, 2025

pytest-bdd is mostly awesome but I badly miss behave's ability to show and select tests by feature file. When a BDD test under pytest-bdd fails, it is an utter pain to find the feature file with the source of the scenario.

This PR uses pytest collection hooks to manually collect feature files into pytest collection items and annotate them with the feature file name. So instead of reporting a rather meaningless:

test/bdd/test_api.py ................................................... [  6%]
................................................ss...................... [ 15%]
........................................................................ [ 25%]
........................................................................ [ 34%]
........................................................................ [ 43%]
............................                                             [ 47%]
test/bdd/test_db.py .................................................... [ 54%]
........................................................................ [ 63%]
......................s...............s................................. [ 72%]
........................................................................ [ 82%]
..s.....................s.............                                   [ 86%]
test/bdd/test_osm2pgsql.py ............................................. [ 92%]
........................................................                 [100%]

we now get

test/bdd/features/api/reverse/v1_geocodejson.feature .................      [  2%]
test/bdd/features/api/reverse/v1_geojson.feature .............              [  3%]
test/bdd/features/api/reverse/geometry.feature .....                        [  4%]
test/bdd/features/api/reverse/v1_json.feature .....................         [  7%]
test/bdd/features/api/reverse/layers.feature .................              [  9%]
test/bdd/features/api/reverse/language.feature ....                         [  9%]
test/bdd/features/api/search/language.feature .......                       [ 10%]
test/bdd/features/api/details/language.feature .......                      [ 11%]
test/bdd/features/api/details/params.feature ................               [ 13%]
test/bdd/features/api/details/simple.feature ........ss...                  [ 15%]

And when running in verbose mode, there are even line numbers:

test/bdd/features/osm2pgsql/import/entrances.feature::L4::test_import_simple_entrance PASSED                                            [  9%]
test/bdd/features/osm2pgsql/import/entrances.feature::L22::test_addresses_and_entrance_information_can_exist_on_the_same_node PASSED    [ 18%]
test/bdd/features/osm2pgsql/import/entrances.feature::L33::test_entrance_import_can_be_disabled[()] PASSED                              [ 27%]
test/bdd/features/osm2pgsql/import/entrances.feature::L33::test_entrance_import_can_be_disabled[(nil)] PASSED                           [ 36%]
test/bdd/features/osm2pgsql/import/entrances.feature::L33::test_entrance_import_can_be_disabled[{}] PASSED                              [ 45%]

You can run a single feature file using expression matching:

pytest test/bdd -k osm2pgsql/import/entrances.feature

Running a single scenario is sadly a bit less straightforward but still works:

pytest test/bdd -k 'osm2pgsql/import/entrances.feature and L4'

Only works with pytest >= 8.0. Older versions of pytest will still work but keep using pytest-bdd's default collector.

@lonvia lonvia merged commit 6a2d2da into osm-search:master Oct 31, 2025
8 checks passed
@lonvia lonvia deleted the improve-bdd-test-names branch October 31, 2025 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant