In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Pytest use case between the two ways to achieve parameter transfer is what, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
preface
When we do interface automation testing, we often encounter this scenario: a field in the return result of interface A is an input parameter of a field of interface B. If we use postman, we can set the postman variable and then call it {{}} where we need it. But how do you implement it if you use your own testing framework? I think of three ways.
First of all, the following three ways are implemented by python + pytest.
Creating global variables through conftest
The conftest.py file is a very useful thing in the pytest framework. First look at the explanation in the official documentation:
This roughly means that the conftest.py file is used by the entire use case directory (conftest.py file can be multiple and only valid under the current package) without import, that is, the information in the use case directory is conftest is common, for example:
a/conftest.py: def pytest_runtest_setup(item): # called for running each test in 'a' directory print("setting up", item) a/test_sub.py: def test_sub(): pass test_flat.py: def test_flat(): pass
Post Run Results:
pytest test_flat.py --capture=no # will not show "setting up"
pytest a/test_sub.py --capture=no # will show "setting up"
And we can use conftest + fixture function to achieve the effect we want, the specific code is as follows:
#conftest.py #Define a global variable to store the contentglobal_data = {} @pytest. fixturefset_global_data(): """ Set global variables for associating parameters :return: """ def _set_global_data(key, value): global_data[key] = value return _set_global_data @pytest.fixturedef get_global_data(): """ Values from global variable global_data :return: """ def _get_global_data(key): return global_data.get(key) return _get_global_data
A brief description of the implementation logic:
First define a variable global_data to receive the results returned by the storage use case
set_global_data and get_global_data are two fixture methods. As the name implies, the set method stores data in global_data, and the get method fetches data from global_data.
The method is implemented, how should it be used specifically? As follows:
# test_get_set.py import requestsimport pytest def test_set(set_global_data): res = requests.get("http://www.baidu.com") status_code = res.status_code logger.info (f"Request return status code: {status_code}") set_global_data("status_code", status_code) def test_get(get_global_data): data = get_global_data("status_code") logger.info (f'value obtained by get_global_data method: {data}') if __name__== '__main__': pytest.main(['-sv', 'test_get_set.py'])
Return Results:
test_get_set.py::test_set PASSED
2021-12-24 17:58:37.642 | INFO |cases.test_get_set:test_set:19 -Request return status code: 200
2021-12-24 17:58:37.643 | INFO |cases.test_get_set:test_get:25 -Value obtained by get_global_data method: 200
test_get_set.py::test_get PASSED
============================== 2 passed in 0.06s ===============================
In this way, the problem of parameter transfer between use cases is realized.
In actual work, because there are many interfaces and use cases involved, you can use different conftests for management according to your needs. And the stored data structure also needs to be standardized, such as using the method name as the key of the dictionary.
Use tmpdir_factory method
The second method is to use pytest's tmpdir and tmpdir_factory fixture functions, which are also implemented through the conftest file. Let's first look at the official documentation for these two methods:
In simple terms, these two methods create a temporary directory for each test method to store custom files. This temporary directory will store 3 sessions by default, and then delete the old directories in the order they were created. Take an official example:
# content of test_tmpdir.pydef test_create_file(tmpdir): p = tmpdir.mkdir("sub").join("hello.txt") p.write("content") assert p.read() == "content" assert len(tmpdir.listdir()) == 1 assert 0# contents of conftest.pyimport pytest @pytest.fixture(scope="session")def image_file(tmpdir_factory): img = compute_expensive_image() fn = tmpdir_factory.mktemp("data").join("img.png") img.save(str(fn)) return fn # contents of test_image.pydef test_histogram(image_file): img = load_image(image_file) # compute and test histogram
My use in actual projects:
It is still a custom fixture function in the conftest.py file, the return result is a tuple, p is the object returned by the tmpdir_factory method, and after being converted to a string, it is the path of the file storage.
Customize a folder named "apitest-tmp-dir" to store files
# conftest.py @pytest.fixturedef tmp_factory(tmpdir_factory): """ Generate temporary catalog """ p = tmpdir_factory.mktemp('apitest-tmp-dir') logger.info ("directory of current temporary file is: " + str(p)) return p, str(p)
Use in test methods
# test_get_set.py import requestsimport pytestimport json def test_set(tmp_factory): res = requests.get("http://www.baidu.com") status_code = res.status_code logger.info (f"return status code: {status_code}") logger.debug(tmp_factory) #Create test_set.txt file a = tmp_factory[0].join("test_set.txt") #Write what you need into the file a.write({"status_code": status_code}) #Get the contents of a file using the read() method logger.debug(a.read()) if __name__ == '__main__': pytest.main(['-sv', 'test_get_set.py'])
Return Results:
test_get_set.py::test_set 2021-12-24 18:24:39.292 | INFO |cases.conftest:tmp_factory:150 -The directory of the current temporary file is: /private/var/folders/_f/1d0lt83x1599bf6mcfppbwp40000gn/T/pytest-of-j/pytest-19/apitest-tmp-dir0
2021-12-24 18:24:39.347 | INFO |cases.test_get_set:test_set:32 -Return status code: 200
2021-12-24 18:24:39.347 | DEBUG | cases.test_get_set:test_set:34 - (local('/private/var/folders/_f/1d0lt83x1599bf6mcfppbwp40000gn/T/pytest-of-j/pytest-19/apitest-tmp-dir0'), '/private/var/folders/_f/1d0lt83x1599bf6mcfppbwp40000gn/T/pytest-of-j/pytest-19/apitest-tmp-dir0')
2021-12-24 18:24:39.348 | DEBUG | cases.test_get_set:test_set:38 - {'status_code': 200}
PASSED
============================== 1 passed in 0.07s ===============================
Files created:
As you can see, tmpdir_factory will automatically create a directory for us with the name of the custom name in the `tmp_factory` method followed by 0, and its parent directory will automatically increment from pytest-0
Here are the advantages and disadvantages of this approach:
First of all, this data store is written directly to a file, so it can be accessed even after the run ends, unlike the first method, where the contents of the store disappear at the end of the use case run.
On the downside, because this temporary directory can only hold up to three files, there is a risk that files will be deleted automatically if there are many use cases. However, this seems to be able to modify the default configuration to solve, you can continue to study the next.
Disadvantage 2: In the above example, the contents of the file are accessed directly through a.read(), because the storage and reading of the contents are all in one method, so they can be called directly. If method A stores the result and needs to read it in method B, then read() cannot be used directly (because each method creates a directory, and the default read address is the directory created by the method itself). We need to package a method to read the file separately, which is not difficult to implement. Moreover, these methods of tmpdir itself are also secondary packaging of some methods of os. path.
Then again, I need to package a way to read files myself, so why not just read and write them myself? In this way, whether to delete files, delete a few, and when to delete them are completely defined by themselves. It seems to be more convenient. You should have some understanding of both methods, the core is through pytest conftes.py file to achieve.
After reading the above, do you know how to implement the two ways of parameter passing between pytest use cases? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!
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.