用unittest从数据生成测试的模块

gentests的Python项目详细描述


使用unittest从数据生成测试的模块

安装:

使用pip安装:

pip install gentests

示例:

以下测试代码将生成10个测试:

import unittest
from gentests import gentests, vals

def my_less_than(a,b):
    '''Broken function to test'''
    return a <= b

@gentests
class TestSomething(unittest.TestCase):


    @vals([
        'val_0',
        'val_1',
        'val_2'
    ])
    def test_False(self, value):
        '''Test that fails'''
        self.assertFalse(True)

    @vals([
        'val_0',
        'val_1',
        'val_2'
    ])
    def test_True(self, value):
        '''Test that passes'''
        self.assertTrue(True)

    def name_test(original_name, left, right, result):
        '''Explicitly name our tests based on our arguments'''
        return '{0}_my_less_than_{1}_{2}_is_{3}'.format(
            original_name,
            str(left),
            str(right),
            str(result)
        )

    @vals([
        (0, 1, True),
        (0, 0, False), #Fails
        (0, 2, True),
        (2, 1, False),
    ], name=name_test) #pass optional naming function
    def test_MyLessThan(self, left, right, result):
        '''Test where some pass and some fail'''
        self.assertEqual(my_less_than(left, right), result)

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

产生以下输出:

FFFF......
======================================================================
FAIL: test_False_gentest_0 (test.TestSomething)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Redirection\windoj9\Documents\repos\GenTests\gentests.py", line 17, in <lambda>
    return lambda other_self: base_func(other_self, *args)
  File "C:\Users\Redirection\windoj9\Documents\repos\GenTests\test.py", line 19, in test_False
    self.assertFalse(True)
AssertionError: True is not false

======================================================================
FAIL: test_False_gentest_1 (test.TestSomething)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Redirection\windoj9\Documents\repos\GenTests\gentests.py", line 17, in <lambda>
    return lambda other_self: base_func(other_self, *args)
  File "C:\Users\Redirection\windoj9\Documents\repos\GenTests\test.py", line 19, in test_False
    self.assertFalse(True)
AssertionError: True is not false

======================================================================
FAIL: test_False_gentest_2 (test.TestSomething)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Redirection\windoj9\Documents\repos\GenTests\gentests.py", line 17, in <lambda>
    return lambda other_self: base_func(other_self, *args)
  File "C:\Users\Redirection\windoj9\Documents\repos\GenTests\test.py", line 19, in test_False
    self.assertFalse(True)
AssertionError: True is not false

======================================================================
FAIL: test_MyLessThan_my_less_than_0_0_is_False (test.TestSomething)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Redirection\windoj9\Documents\repos\GenTests\gentests.py", line 17, in <lambda>
    return lambda other_self: base_func(other_self, *args)
  File "C:\Users\Redirection\windoj9\Documents\repos\GenTests\test.py", line 47, in test_MyLessThan
    self.assertEqual(my_less_than(left, right), result)
AssertionError: True != False

----------------------------------------------------------------------
Ran 10 tests in 0.006s

FAILED (failures=4)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在删除后修复recyclerView中的项目重复?   java如何在Android中使用Handle而不增加内存   java(i>>>距离)是如何工作的   java如何在菜单项操作之后删除JTabbedPane的内容   springintermediatejavax。ejb。EJBException:java。Jboss应用程序中的lang.NullPointerException   Java:派生类中的方法链接   java InputListener不适用于正交摄影机和角色   java不能写这个方法吗?   java为什么Apache Kafka消费者不使用来自主题的消息?   使用scanner Java从文本文件填充二维数组   爪哇在会场内放置标记   maven合并了2个Java web应用程序   Java中注释处理的缺点是什么?   java创建在JFrame中绘制矩形和圆形的方法?   java LibGDX应用程序挂起在initializeglfw()上   唯一包含密钥但在不同字段上排序的java集   jboss在使用Infinispan中的共享文件存储时获取“java.io.IOException:不支持的协议版本22”   JavaEclipse似乎不想编译我的类的新版本。即使在(我认为)修复它之后,我也会遇到同样的错误