我的单元测试是否编写正确以测试我的代码功能

时间:2019-12-02 16:30:14

标签: python unit-testing pytest

尝试编写单元测试来测试我的Python代码项目。

我有一个gestioner.py文件,该文件检查User输入并查看(从“允许的列表”中)是否有效(从“ danger_list”中) 然后还要检查输入的命令是否输入了可疑字符以防止安全攻击。

现在我想将单元测试添加到Gestioner.py中的功能代码中

遵循Stackoverflow问题中的文章:Writing unit tests in Python: How do I start?

然后是py.test文档(https://docs.pytest.org/en/latest/contents.html

并且我写了以下test_gestioner.py来开始我的单元测试,如下所示:

我的所有三个测试都一直在显示“正在逐渐消失”。但是我怀疑我的这些测试是否真正正确地测试了我的代码,因为当我期望测试失败时,该测试仍会通过。

我正确地对此代码进行了单元测试吗? 在我看来,我不是,所以请建议在单元测试中我在哪里做错了?

这是我的主要脚本Gestioner.py



import sys
import re


class CmdGestioner:
    _allowed_list = ['fichier', 'binfile', 'prep', 'rep' ]
    _danger_list = ['gen', 'regen'] 

    def __init__(self):
        None

    def chk_cmds(self, mycmd):
        print('chk_cmds: lets look term at {}'.format(mycmd))
        regex = re.compile(r'[&`%;$><!#]')
        msg = bool(regex.search(mycmd))
        print('My command: ' +mycmd)
        if msg is True:
            print(msg)
            print('Found a suspicious entry in the command : {}'.format(mycmd))
            return -1
        else:
            print('Command seems clean {}'.format(mycmd))
            return 0


    def is_valid(self, in_command):
        valid = True


        cmd_mtg_str = ''.join(str(elem) for elem in in_command)



        for term in cmd_mtg_str.split():

            if term.strip('--') not in self._allowed_list:
                if term.strip('--') in self._forbidden_list:
                    valid = False
                    print('Found a forbidden Sec_Service command '+term)
                else:
                    print ('%s not found in list so need to runs checks on this!' % (term))
                    checkResult = self.chk_cmds(term)
                    if checkResult == -1:
                        valid = False
            else:
                print ('Found in list so term %s is valid' % (term))
        return valid




test_command = ' '.join(str(elem) for elem in sys.argv[1:])
cmd = CmdGestioner()

status = cmd.is_valid(test_command)
print ('\nStatus for command: %s is %s' % (test_command,status))

和我的单元测试文件(test_gestioner.py)

import sys
import re
import unittest
import pytest
from gestioner import CmdGestioner

cmd = CmdGestioner()


def test_chk_cmds_valid():
    mycmd = "--binfile testbinfile"
    cmd.chk_cmds(mycmd)
    assert 'Status for command: ' +mycmd +' is True'


def test_chk_cmds_danger():
    mycmd = "--gen genf"
    cmd.chk_cmds(mycmd)
    assert 'Status for command: ' +mycmd +' is False'


def test_chk_cmds_attacks():
    mycmd = ";ls"
    cmd.chk_cmds(mycmd)
    assert 'Found a suspicious entry in the command : ' .format(mycmd)
    assert 'Status for command: ' +mycmd +' is False


正在执行我的单元测试:

PS C:\Work\Questions_forums\SecService_messaging> py.test -v                                                                                                                                                                                       ================================================================================== test session starts =============================================================================================================================
platform win32 -- Python 3.7.4, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 -- c:\users\SecUser\appdata\local\programs\python\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.4', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '5.0.1', 'py': '1.8.0', 'pluggy': '0.12.0'}, 'Plugins': {'html': '1.21.1', 'metadata': '1.8.0'}}
rootdir: C:\Work\Questions_forums\SecService_messaging
plugins: html-1.21.1, metadata-1.8.0
collected 3 items

test_gestioner.py::test_chk_cmds_valid PASSED                                                                                                                                                                                                                           [ 33%]
test_gestioner.py::test_chk_cmds_danger PASSED                                                                                                                                                                                                                          [ 66%]
test_gestioner.py::test_chk_cmds_attacks PASSED                                                                                                                                                                                                                         [100%]

========================================================================================================================== 3 passed in 0.03 seconds ==========================================================================================================================

1 个答案:

答案 0 :(得分:1)

否,您的单元测试书写不正确。 chk_cmd方法不返回字符串,而是按标准输出,但是您的assert语句正在寻找chk_cmd的结果等于所打印的结果。相反,assert语句将检查字符串是否等于true,其中一个宪章或以上的任何字符串都被视为true,因此这些测试应始终通过。为了使assert语句在编写时起作用,您需要从chk_cmd方法返回预期的字符串或测试其他内容。例如

assert cmd.is_valid('rep')
assert cmd.chk_cmds(mycmd) == -1
assert cmd.chk_cmd(mycmd) == 0

看起来您可能来自C编程背景,返回值为-1和0。如果将这些值重写为True和False,将会改善代码和断言。以下重构:

class CmdGestioner:
    _allowed_list = ['fichier', 'binfile', 'prep', 'rep' ]
    _danger_list = ['gen', 'regen']

    def chk_cmds(self, mycmd):
        regex = re.compile(r'[&`%;$><!#]')
        msg = bool(regex.search(mycmd))
        if msg is True:
            return False
        else:
            return True


    def is_valid(self, in_command):
        valid = True
        cmd_mtg_str = ''.join(str(elem) for elem in in_command)
        for term in cmd_mtg_str.split():
            if term.strip('--') not in self._allowed_list:
                if term.strip('--') in self._danger_list:
                    valid = False
                else:
                    if self.chk_cmds(term):
                        valid = False
        return valid

更具可读性(省略了打印语句以更清楚地看到建议的更改),并且允许将断言重写为

assert cmd.chk_cmds(mycmd)

因为该方法现在返回true或false,所以您不必在断言中添加条件语句。

由于您正在检查字符串而不是标准输出,因此拥有当前断言的方式应始终评估为true。