将标题从Cucumber的数据表顶部移到一边 - Python

时间:2017-05-03 09:40:22

标签: python automated-tests cucumber python-behave

我正在寻找改变黄瓜数据表标题的方法。因此它将使特征文件可读。

普通方式:

| Name | Email   | Phone No. | ......... |
| John | i@g.net | 098765644 | ......... |

它可以是一个非常宽的数据表,我必须来回滚动。

期望的方式:

| Name      | John      |
| Email     | i@g.net   |
| Phone No. | 098765444 |
.
.
.

Java和Ruby中有少量示例。但我正在使用Python。

我尝试了许多不同的东西,比如numpy.transpose(),将它们转换为list。但它不起作用,因为数据表的格式是:

[<Row['Name','John'],...]

2 个答案:

答案 0 :(得分:0)

这看起来与numpy无关。

通常使用zip(* the_list)

来转动列表列表

这将返回一个透视的行为表

from behave.model import Table


class TurnTable(unittest.TestCase):
    """ 
    """

    def test_transpose(self):
        table = Table(
            ['Name', 'John', 'Mary'],
            rows=[
                ['Email', "john@example.com", "mary@example.com"],
                ['Phone', "0123456789", "9876543210"],
            ])

        aggregate = [table.headings[:]]
        aggregate.extend(table.rows)
        pivoted = list(zip(*aggregate))
        self.assertListEqual(pivoted,
                             [('Name', 'Email', 'Phone'),
                              ('John', 'john@example.com', '0123456789'),
                              ('Mary', 'mary@example.com', '9876543210')])

        pivoted_table = Table(
            pivoted[0],
            rows=pivoted[1:])
        mary = pivoted_table.rows[1]
        self.assertEqual(mary['Name'], 'Mary')
        self.assertEqual(mary['Phone'], '9876543210')

您还可以查看https://pypi.python.org/pypi/pivottable

答案 1 :(得分:0)

您可以非常简单地实现此行为,这是我的版本:

def tabledict(table, defaults, aliases = {}):
"""
    Converts a behave context.table to a dictionary.
    Throws NotImplementedError if the table references an unknown key.

    defaults should contain a dictionary with the (surprise) default values.
    aliases makes it possible to map alternative names to the keys of the defaults.
    All keys of the table will be converted to lowercase, you sould make sure that
    you defaults and aliases dictionaries also use lowercase.

    Example:
        Given the book
          | Property                             | Value              |
          | Title                                | The Tragedy of Man |
          | Author                               | Madach, Imre       |
          | International Standard Book Number   | 9631527395         |

    defaults = { "title": "Untitled", "author": "Anonymous", "isbn": None, "publisher": None }
    aliases = { "International Standard Book Number" : "isbn" }
    givenBook = tabledict(context.table, defaults, aliases)

    will give you:
    givenBook == {
        "title": "The Tragedy of Man",
        "author": "Madach, Imre",
        "isbn": 9631527395,
        "publisher": None
        }
"""
initParams = defaults.copy()
validKeys = aliases.keys()[:] + defaults.keys()[:]
for row in table:
    name, value = row[0].lower(), row[1]
    if not name in validKeys:
        raise NotImplementedError(u'%s property is not supported.'%name)

    if name in aliases: name = aliases[name]
    initParams[name] = value
return initParams