Search code examples
pythonautomated-testscucumberpython-behave

Move the headings from top of Cucumber's Data Table to side - Python


I am looking for the ways to change the headings of Cucumber's Data Table to the side. So it will make the feature file readable.

Ordinary way:

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

It can be a very wide data table and I would have to scroll back and forth.

Desired way:

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

There are a small number of examples in Java and Ruby. But I am working with Python.

I had tried many different things like numpy.transpose(), converting them to list. But it won't work because the Data Table's format is:

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

Solution

  • This doesn't look like it's related to numpy.

    pivoting a list of list is often done with zip(*the_list)

    This will return a pivoted behave table

    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')
    

    you can also have a look at https://pypi.python.org/pypi/pivottable