Python data-driven DDT use

Source: Internet
Author: User
Tags unpack writing test scripts

Ready to work: Pip Install DDT

Knowledge Points:

One, data-driven and code-driven:

Data-driven means testing according to the data you provide, such as the ATP Framework requires test cases in Excel

Code-driven is the code that must be written to test, say, UnitTest.

Second, the meaning of using the data-driven framework:
-High code reuse rate. The same test logic is written once, can be reused by multiple test data, improve the reuse rate of test code, and can improve the efficiency of writing test scripts.
-High efficiency of abnormal troubleshooting. The test framework generates one test case per data based on the test data, and the use case execution is isolated from each other, without affecting the other test cases in the case of one failure.

-The code is highly maintainable. The clear testing framework facilitates reading by other test engineers and improves the maintainability of the code.

Tips : Few parameters with yml very good parameters, with the JSON Xx.json format of the upper and lower dependencies on the write PY file

The file name, JSON, is the JSON file in which to write the JSON string.

Yml each interface is run independently

DDT use

DDT contains a class of adorners DDT and two method adorner data (direct input test data), File_data (can get test data from JSON or YAML)

Only files ending in Yaml and YML are formatted in Yaml, and in other cases JSON is the default

Typically, data is passed to the test case by one parameter, and if data contains multiple data, tuples, lists, dictionaries, and so on, it is necessary to decompose the data in the script or use unpack to decompose the data.

@data (A, B)

Then A and B each run the use case

@data ([a,b],[c,d])

If there is no unpack, then [A, b] is passed into the use case as a parameter run

If there is a unpack, then [A, b] is decomposed and passed according to two parameters in the use case

@file_data (filename)

For JSON files, each JSON element runs according to a use case, and can be broken down into the way that Python decomposes tuples, lists, or dictionaries.

Instance:

Import DDT
Import UnitTest
# Print (dir (DDT))

@ddt. DDT
class Mycase (unittest. TestCase):
@ddt. Data (#运行2次)
def Testa (self,value):
print (value)

@ddt. Data ([up]) #运行1次
def testb (self,value):
print (value)

@ddt. Data ([1,2],[3,4]) #不加unpack will error
def TESTC (self,a,b):
Self.assertnotequal (A, b)

@ddt. Data ([1,2],[3,4]) #运行2次
@ddt. Unpack
def testd (self,a,b):
Self.assertnotequal (A, b)

if __name__ = = ' __main__ ':
Unittest.main ()

Python data-driven DDT use

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.