In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.