2016-07-08 1 views
0

は、私は次のように定義されたステップがあるとしどのようにして、Pythonの動作ステップデータテーブルの列の型を指定できますか?</p> <pre><code>Then I would expect to see the following distribution for Ford | engine | doors | color | | 2.1L | 4 | red | </code></pre> <p>そして、私はテーブルを読み込み、次のように表明を行うステップの実装を持っています:

@then('I would expect to see the following distribution for {car_type}') 
def step(context, car_type): 
    car = find_car_method(car_type) 
    for row in context.table: 
     for heading in row.headings: 
      assertEqual(getattr(car, heading), 
         row[heading], 
         "%s does not match. " % heading + \ 
         "Found %s" % getattr(car, heading)) 

(私はこのように、このようにそれを行いますアプローチはより多くのフィールドを追加することを可能にしますが、車の属性のチェックの多くの用途に十分な汎用性を保ちます)。

私の車のオブジェクトが4つのドア(int)を持っている場合、データテーブルが '4'のドア(ユニコードstr)を必要とするため、一致しません。

私はこのメソッドを実装して、列の名前をチェックし、フィールドごとに違った扱いをすることができますが、新しいフィールドを追加するときは、追加する場所が1つ増えるのでメンテナンスが難しくなります。代わりにステップデータテーブルに指定することをお勧めします。何かのように:

Then I would expect to see the following distribution for Ford 
     | engine | doors:int | color | 
     | 2.1L | 4   | red | 

私はこれを達成するために使用することができます(これは動作しません)ですか?

同じ問題が発生しているデータテーブルから作成する必要がある場合があります。このmakeは、 'car'オブジェクトの型をそのまま使用して型を決定しようとしても役に立たない。

は、いくつかの掘削後、私は何かを見つけることができませんでしたので、私自身のソリューションを実装することを決定した

ベール

答えて

0

、ありがとうございます。私は将来誰かを助けるかもしれないので、ここに投稿しています。次のように私は、その後のシナリオで設定することができ

def convert_to_type(full_field_name, value): 
    """ Converts the value from a behave table into its correct type based on the name 
     of the column (header). If it is wrapped in a convert method, then use it to 
     determine the value type the column should contain. 

     Returns: a tuple with the newly converted value and the name of the field (without the 
       convertion method specified). E.g. int(size) will return size as the new field 
       name and the value will be converted to an int and returned. 
    """ 
    field_name = full_field_name.strip() 
    matchers = [(re.compile('int\((.*)\)'), lambda val: int(val)), 
       (re.compile('float\((.*)\)'), lambda val: float(val)), 
       (re.compile('date\((.*)\)'), lambda val: datetime.datetime.strptime(val, '%Y-%m-%d'))] 
    for (matcher, func) in matchers: 
     matched = matcher.match(field_name) 
     if matched: 
      return (func(value), matched.group(1)) 
    return (value, full_field_name) 

Then I would expect to see the following distribution for Ford 
    | engine | int(doors) | color | 
    | 2.1L | 4   | red | 

次のように私はその後、ステップ変更:

@then('I would expect to see the following distribution for {car_type}') 
def step(context, car_type): 
    car = find_car_method(car_type) 
    for row in context.table: 
     for heading in row.headings: 
      (value, field_name) = convert_to_type(heading, row[heading]) 
      assertEqual(getattr(car, field_name), 
         value, 
         "%s does not match. " % field_name + \ 
         "Found %s" % getattr(car, field_name)) 

それは

私はヘルパーメソッドを作成しましたメソッドが呼び出されるたびに再作成する必要がないため、「matcher」をモジュールレベルに移動する必要があります。また、エンジンサイズを解析して標準ユニットに変換するための変換メソッド(例:liter()やcc()など)を増やすには、これを拡張するのも簡単です。

関連する問題

 関連する問題