Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.6单元测试期间防止代码退出_Python_Python 3.x - Fatal编程技术网

如何在Python 3.6单元测试期间防止代码退出

如何在Python 3.6单元测试期间防止代码退出,python,python-3.x,Python,Python 3.x,当我使用Python3.6编写函数和类时,我会尽可能快地编写单元测试。我正在编写一个简单的函数,用于测试文件是否存在于用户指定的位置,如下所示: import os import sys import unittest def verify_file_existance(file_path, file_name): total_name = file_path + file_name if os.path.isfile(total_name): return

当我使用Python3.6编写函数和类时,我会尽可能快地编写单元测试。我正在编写一个简单的函数,用于测试文件是否存在于用户指定的位置,如下所示:

import os
import sys
import unittest

def verify_file_existance(file_path, file_name):
    total_name = file_path + file_name
    if os.path.isfile(total_name):
        return
    sys.exit('{}{}{}'.format('FATAL ERROR: ', file_name, 'does not exist'))
上述函数的单元测试如下所示

class InputFileTest(unites.TestCase):
    def test_verify_file_existance(self):
        file_name = 'Test.csv'
        file_path = '../../Data/VandV/Input_Reader/'
        verify_file_existance(file_path, file_name)

如果文件存在于所需位置,则测试成功通过;但是,如果文件不存在,程序将退出并发出致命警告。这就是我希望程序在正常运行期间的行为方式;然而,在单元测试中,我希望能够有目的地放置一个不正确的路径,并将其注册为一个成功的测试,以证明程序做了我想要的事情。换句话说,在一个测试用例中,我不希望程序退出。在单元测试期间有没有办法做到这一点?

您可以使用try/except优雅地处理错误


尝试执行Try-except语句:

class InputFileTest(unites.TestCase):
    def test_verify_file_existance(self):
        try:
            file_name = 'Test.csv'
            file_path = '../../Data/VandV/Input_Reader/'
            verify_file_existance(file_path, file_name)
        except Exception as e:
            print("\n\aError. Unable to locate File!\nError: {}").format(str(e))
            try:
                exit(0)
            except:
                sys.exit(1)
你调查过了吗?
class InputFileTest(unites.TestCase):
    def test_verify_file_existance(self):
        try:
            file_name = 'Test.csv'
            file_path = '../../Data/VandV/Input_Reader/'
            verify_file_existance(file_path, file_name)
        except Exception as e:
            print("\n\aError. Unable to locate File!\nError: {}").format(str(e))
            try:
                exit(0)
            except:
                sys.exit(1)