pytest-dev / pytest-bdd

BDD library for the py.test runner
https://pytest-bdd.readthedocs.io/en/latest/
MIT License
1.3k stars 219 forks source link

Cardinality Field usage not documented :( #517

Closed edmondop closed 2 years ago

edmondop commented 2 years ago

I am trying to make a reusable step for an expression like the following:

a list of values 1e-5 1e-6 1e-7

The documentation mention cfparse as an enhanced parser which support quantifiers:

cfparse (extends: pypi_parse, based on: pypi_parse_type)

Provides an extended parser with “Cardinality Field” (CF) support. Automatically creates missing type converters for related cardinality as long as a type converter for cardinality=1 is provided. Supports parse expressions like: {values:Type+} (cardinality=1..N, many) {values:Type} (cardinality=0..N, many0) {value:Type?} (cardinality=0..1, optional) Supports type conversions (as above).

So I imagined I could write code like the following:

    from pytest_bdd import parsers
    my_parser = parsers.cfparse("a list of values {my_value:P*}, extra_types=dict(P=float))
    my_parser.parse_arguments('a list of values 3 4 5')

and only managed to get an exception:

could not convert string to float: '3 4 5'

The documentation is not very clear, and no examples or unit tests provide help on how to use the cardinality field. Clearly, I am not using in the right way :(

elchupanebrej commented 2 years ago

Please check https://github.com/jenisys/parse_type

dcendents commented 2 years ago

The parsers expects a coma , to separate each value, so you would need to write your statement as a list of values 3, 4, 5.

You could also register your own list separator instead, I needed to do that once because my values contained a coma: extra_types=dict(strlist=TypeBuilder.with_many(str, listsep="|") So basically I say strlist is many strings str (1 or more, +) separated by |.

I haven't tested it but I would think the following could work for you:

parsers.cfparse("a list of values {my_value:P}, extra_types=dict(P=TypeBuilder.with_many0(float, listsep=" ")))
youtux commented 2 years ago

I'll close this, as it seems unrelated to pytest-bdd.

mostafa-asg commented 2 months ago

Thanks @dcendents