pythonddt是什么_python ddt

本文介绍了Python的ddt库在单元测试中的应用,包括使用ddt.data和ddt.file_data装饰器来创建多组测试用例,以及如何通过ddt.unpack进行参数展开。示例代码展示了如何为测试方法提供列表、字典和JSON文件中的数据,并打印测试结果。
摘要由CSDN通过智能技术生成

#!/usr/bin/env/python

# -*- coding: utf-8 -*-

# @Time : 2018/12/15 15:27

# @Author : ChenAdong

# @Email : aiswell@foxmail.com

import unittest

import ddt

lst = [1, 2, 3]

dic = {"userName": "chen"}

tur = (1, 2, 3)

s = {1, 2, 3}

@ddt.ddt

class Test(unittest.TestCase):

@ddt.data(*lst)

def test_list(self, data):

print("test_list")

print(data)

print("==================")

@ddt.data(*dic)

def test_dictionary(self, data):

print("test_dic")

print(data)

print("==================")

@ddt.file_data("ddt_test001.json")

def test_file(self, key):

print(key)

@ddt.file_data("ddt_test.json")

@ddt.unpack

def test_file(self, start, end, value):

print(start, end, value)

if __name__ == "__main__":

unittest.main()

"""

# 付上ddt-help

E:\myworkspace\python_workspace\tools\venv\Scripts\python.exe E:/myworkspace/python_workspace/projects/tmp/test002.py

Help on module ddt:

NAME

ddt

DESCRIPTION

# -*- coding: utf-8 -*-

# This file is a part of DDT (https://github.com/txels/ddt)

# Copyright 2012-2015 Carles Barrobés and DDT contributors

# For the exact contribution history, see the git revision log.

# DDT is licensed under the MIT License, included in

# https://github.com/txels/ddt/blob/master/LICENSE.md

FUNCTIONS

add_test(cls, test_name, test_docstring, func, *args, **kwargs)

Add a test case to this class.

The test will be based on an existing function but will give it a new

name.

data(*values)

Method decorator to add to your test methods.

Should be added to methods of instances of ``unittest.TestCase``.

ddt(cls)

Class decorator for subclasses of ``unittest.TestCase``.

Apply this decorator to the test case class, and then

decorate test methods with ``@data``.

For each method decorated with ``@data``, this will effectively create as

many methods as data items are passed as parameters to ``@data``.

The names of the test methods follow the pattern

``original_test_name_{ordinal}_{data}``. ``ordinal`` is the position of the

data argument, starting with 1.

For data we use a string representation of the data value converted into a

valid python identifier. If ``data.__name__`` exists, we use that instead.

For each method decorated with ``@file_data('test_data.json')``, the

decorator will try to load the test_data.json file located relative

to the python file containing the method that is decorated. It will,

for each ``test_name`` key create as many methods in the list of values

from the ``data`` key.

feed_data(func, new_name, test_data_docstring, *args, **kwargs)

This internal method decorator feeds the test data item to the test.

file_data(value)

Method decorator to add to your test methods.

Should be added to methods of instances of ``unittest.TestCase``.

``value`` should be a path relative to the directory of the file

containing the decorated ``unittest.TestCase``. The file

should contain JSON encoded data, that can either be a list or a

dict.

In case of a list, each value in the list will correspond to one

test case, and the value will be concatenated to the test method

name.

In case of a dict, keys will be used as suffixes to the name of the

test case, and values will be fed as test data.

idata(iterable)

Method decorator to add to your test methods.

Should be added to methods of instances of ``unittest.TestCase``.

is_trivial(value)

mk_test_name(name, value, index=0)

Generate a new name for a test case.

It will take the original test name and append an ordinal index and a

string representation of the value, and convert the result into a valid

python identifier by replacing extraneous characters with ``_``.

We avoid doing str(value) if dealing with non-trivial values.

The problem is possible different names with different runs, e.g.

different order of dictionary keys (see PYTHONHASHSEED) or dealing

with mock objects.

Trivial scalar values are passed as is.

A "trivial" value is a plain scalar, or a tuple or list consisting

only of trivial values.

process_file_data(cls, name, func, file_attr)

Process the parameter in the `file_data` decorator.

unpack(func)

Method decorator to add unpack feature.

DATA

DATA_ATTR = '%values'

FILE_ATTR = '%file_path'

UNPACK_ATTR = '%unpack'

index_len = 5

trivial_types = (, , ,

VERSION

1.2.1

FILE

e:\myworkspace\python_workspace\tools\venv\lib\site-packages\ddt.py

None

Process finished with exit code 0

"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值