Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用";中的tests.py文件本地测试代码;勾选iO";游戏_Python_Unit Testing_Testing - Fatal编程技术网

Python 如何使用";中的tests.py文件本地测试代码;勾选iO";游戏

Python 如何使用";中的tests.py文件本地测试代码;勾选iO";游戏,python,unit-testing,testing,Python,Unit Testing,Testing,在“平台”上,其中一项任务是“简单”任务。作为解决方案,我编写了以下工作代码: def index_power(array, n): """ Find Nth power of the element with index N. """ return array[n] ** n if len(array) > n else -1 if __name__ == '__main__': #These "asserts" using only f

在“平台”上,其中一项任务是“简单”任务。作为解决方案,我编写了以下工作代码:

def index_power(array, n):
    """
        Find Nth power of the element with index N.
    """
    return array[n] ** n if len(array) > n else -1

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert index_power([1, 2, 3, 4], 2) == 9, "Square"
    assert index_power([1, 3, 10, 100], 3) == 1000000, "Cube"
    assert index_power([0, 1], 0) == 1, "Zero power"
    assert index_power([1, 2], 3) == -1, "IndexError"
对于此任务,GitHub页面上有一个用于测试的测试文件。这里有一个链接=>“”。此文件包含以下代码:

"""
TESTS is a dict with all you tests.
Keys for this will be categories' names.
Each test is dict with
    "input" -- input data for user function
    "answer" -- your right answer
    "explanation" -- not necessary key, it's using for additional info in animation.
"""

TESTS = {
    "Basics": [
        {
            "input": ([1, 2, 3, 4], 2),
            "answer": 9,
        },
        {
            "input": ([1, 3, 10, 100], 3),
            "answer": 1000000,
        },
        {
            "input": ([0, 1], 0),
            "answer": 1,
        },
        {
            "input": ([1, 2], 3),
            "answer": -1,
        },
    ],
    "Extra": [
        {
            "input": ([0], 0),
            "answer": 1,
        },
        {
            "input": ([1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 9),
            "answer": 1,
        },
    ]
}

我的问题是:是否可以使用该测试文件在我自己的计算机上测试我的程序?如果可以,我如何实现和运行测试?

它应该一点也不复杂。正如Jornsharpe所说的,只需重复案例

def run_case(input, answer):
    assert index_power(*input) == answer, 'Failed'  # Program stopped on first fail


def test_index_power():
    for level, cases in TESTS.iteritems():  # You can just iterate through dict
        print "Testing level is: %s" % level
        for each in cases:                  # And then through test cases on each level
            run_case(**each)


if __name__ == '__main__':
    test_index_power()

是的,这是可能的-docstring解释了
测试
字典的格式。您需要编写一些代码来迭代测试并将它们应用到您的函数中,但这并不十分复杂。非常感谢您的解决方案,尤其是现成的代码!:-)