Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use pytest to realize Test data driver in Test driven Technology Series

Shulou Source: shulou.com Published: 2022-06-03 04:20:57 09月13日 Update

This article focuses on "Test-driven technology series how to use pytest to achieve test data-driven", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "test-driven technology series how to use pytest to achieve test data-driven" bar!

A set of parameterized data

Define parameterized data as follows:

Class TestDemo1: @ pytest.mark.parametrize ('actual_string, expect_string', [(1,1), (' BB', 'BB'), (' AA', 'BB')]) def test_1 (self, actual_string, expect_string): assert (expect_string = = actual_string)

The running results are as follows: three sets of data were run in three test cases, of which the data ('AA',' BB') failed!

Multiple sets of parametric data

In a test class, you can define multiple sets of parameterized data (two for test_ 1 and three for test_2). The code is as follows:

Class TestDemo1: @ pytest.mark.parametrize ('actual_string, expect_string', [(1, 1), (' BB', 'BB'), (' AA', 'BB')]) def test_1 (self, actual_string, expect_string): assert (expect_string = = actual_string) @ pytest.mark.parametrize (' result, a journal, [(1, 1), (2, 1)] def test_2 (self, result ): assert (result = = aforb)

The running results are as follows: the two sets of data are run in test_1 and test_2 respectively!

Read data from excel as parameters

We can customize some methods to read the external file, and then take the read data as parameters in the pytest

Referenced in the. Save the test data in excel, as shown below

Write a method to read the excel class file, use the module pandas, use the command pip install pandas to install the module, the source code is as follows:

Import pandas as pd # read Excel file-- Pandas def read_data_from_pandas (excel_file, sheet_name): if not os.path.exists (excel_file): raise ValueError ("File not exists") s = pd.ExcelFile (excel_file) df = s.parse (sheet_name) # parse the data of the sheet page return df.values.tolist () # data is returned as list

Read data from excel and assign values to variables for parameterization. The code is as follows:

@ pytest.mark.parametrize ('actual_string, expect_string', read_data_from_pandas (' ErigerTestData.xlswatches, 'data1')) def test_3 (self, actual_string, expect_string): assert (expect_string = = actual_string)

The running results are as follows: three sets of data are run in three test cases!

Note: the first line in excel is not processed as test data by default.

At this point, I believe you have a deeper understanding of the "test-driven technology series of how to use pytest to achieve test data-driven". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Data test parameter run drive technology code file method result content module three learning different practical deeper three number interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft vpn NVidia Redmi Xiaomi