In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.