Reading and writing of use case parameterized ddt&&json file

Steps for ddt:

1,from ddt import ddt, data
2. Use @ ddt on the test class (@ is the syntax of the decorator. Don't delve into it. Just learn to use it now)
3. On the test case method, @ data, and pass in the test data
4. ddt will generate test cases according to the parameters passed in data (one test case will be generated for each parameter)
5. Define a parameter in the test method to receive the case data passed from data

import unittest
from ddt import ddt, data
@ddt
class TestDemo(unittest.TestCase):
    @data(1, 22, 3, 4, 5, 6, 7, 8, 9)
    def test_login(self, item):
        print('item:', item)
        
==============================
cases Is a list of all use case data, and each data in the list is a test case
cases = [
    {'params': {'username': "python3522"},
     "expected": {"code": 1, "msg": "Incorrect account or password"}
     },
    {'params': {'username': "python3522"},
     "expected": {"code": 2, "msg": "Incorrect account or password"}
     },
    {'params': {'username': "python3522"},
     "expected": {"code": 3, "msg": "Incorrect account or password"}
     }
]
@ddt
class TestDome(unittest.TestCase):

    @data(*cases)
    def test_login(self, item):
        print(item)

json file

The difference between json format data and python data:
List in python: [] --- > is called Array in json
Dictionary in python: {key: value} ---- > is called object in json
Boolean value in python: true false ----- > Boolean value in json: true false
Null value in python: none ---- > Boolean value null in json
The quotation marks in json use double quotation marks uniformly

Conversion of data types in json and python
1,json.dumps: converting data from python to JSON data
2,json.loads: convert JSON data into data in python
3,json.load: load the JSON file and convert it to the data format corresponding to python
4,json.dump: write python data into JSON file (automatic conversion format)

"""
import json

json data is in the form of string in python

The difference between json format data and python data:
List in python: [] --- > is called Array in json
Dictionary in python: {key: value} ---- > is called object in json
Boolean value in python: true false ----- > Boolean value in json: true false
Null value in python: none ---- > Boolean value null in json
The quotation marks in json use double quotation marks uniformly

Conversion of data types in json and python
1,json.dumps: converting data from python to JSON data
2,json.loads: convert JSON data into data in python
3,json.load: load the JSON file and convert it to the data format corresponding to python
4,json.dump: write python data into JSON file (automatic conversion format)

"""
import json

json data

The difference between json format data and python data

:
List in python: [] --- > is called Array in json
Dictionary in python: {key: value} ---- > is called object in json
Boolean value in python: true false ----- > Boolean value in json: true false
Null value in python: none ---- > Boolean value null in json
The quotation marks in json use double quotation marks uniformly

Conversion of data types in json and python

1,json.dumps: converting data from python to JSON data
2,json.loads: convert JSON data into data in python
3,json.load: load the JSON file and convert it to the data format corresponding to python
4,json.dump: write python data into JSON file (automatic conversion format)

json data is in the form of string in python

1. Convert python data to corresponding json data
python data

li = [None, False, {'aa': True}]
res = json.dumps(li)
print(res, type(res))

2. Convert json data to data in python

jstr = '[null, false, {"aa": true}]'
res = json.loads(jstr)
print(res, type(res))

3. Load the json file and convert it to the data format corresponding to python

with open(r'C:\project\py41_class\py41_day16\data\test.json', 'r', encoding='utf-8')as f:
    res = json.load(f)

print(res)

4. Write python data into json file (automatic conversion format)

data = {
    "name": "musen", "age": True, "sex": False, "price": None
}
with open(r'C:\project\py41_class\py41_day16\data\data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f)

Added by nec9716 on Fri, 14 Jan 2022 08:23:17 +0200