Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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 3.x 代码覆盖率,函数的Unittest在Python中引发异常 class Kill\u任务: 定义初始化(自): self.action=无 self.cmd=[] def kill_任务(自我): self.action=False #exp task=chrome.exe 目标='chrome.exe' 尝试: self.cmd=supprocess.run(['taskkill','/im',target',/f'],capture\u output=True,check=True) self.action=True 除subprocess.CalledProcessError为e外: 打印(e) self.action=False return self.cmd 需要def操作(自身): 尝试=3 对于范围内的i(尝试): 尝试: self.kill_任务() self.action=True 除subprocess.CalledProcessError为e外: self.action=False 如果我_Python 3.x_Unit Testing_Subprocess_Code Coverage - Fatal编程技术网

Python 3.x 代码覆盖率,函数的Unittest在Python中引发异常 class Kill\u任务: 定义初始化(自): self.action=无 self.cmd=[] def kill_任务(自我): self.action=False #exp task=chrome.exe 目标='chrome.exe' 尝试: self.cmd=supprocess.run(['taskkill','/im',target',/f'],capture\u output=True,check=True) self.action=True 除subprocess.CalledProcessError为e外: 打印(e) self.action=False return self.cmd 需要def操作(自身): 尝试=3 对于范围内的i(尝试): 尝试: self.kill_任务() self.action=True 除subprocess.CalledProcessError为e外: self.action=False 如果我

Python 3.x 代码覆盖率,函数的Unittest在Python中引发异常 class Kill\u任务: 定义初始化(自): self.action=无 self.cmd=[] def kill_任务(自我): self.action=False #exp task=chrome.exe 目标='chrome.exe' 尝试: self.cmd=supprocess.run(['taskkill','/im',target',/f'],capture\u output=True,check=True) self.action=True 除subprocess.CalledProcessError为e外: 打印(e) self.action=False return self.cmd 需要def操作(自身): 尝试=3 对于范围内的i(尝试): 尝试: self.kill_任务() self.action=True 除subprocess.CalledProcessError为e外: self.action=False 如果我,python-3.x,unit-testing,subprocess,code-coverage,Python 3.x,Unit Testing,Subprocess,Code Coverage,除最后一个功能“测试\操作\必需\异常”外,上述所有功能均正常工作。函数应该返回False而不是True。 我正在为test\u action\u required\u异常函数进行代码覆盖。但我无法检查异常,因为它总是正确的。我需要模拟还是不需要能够测试异常?请帮帮我!谢谢@slideshowp2嗨,如果可以的话,你能帮我完成最后一个功能吗 class Kill_Tasks: def __init__(self): self.action = None self.c

除最后一个功能“测试\操作\必需\异常”外,上述所有功能均正常工作。函数应该返回False而不是True。
我正在为test\u action\u required\u异常函数进行代码覆盖。但我无法检查异常,因为它总是正确的。我需要模拟还是不需要能够测试异常?请帮帮我!谢谢

@slideshowp2嗨,如果可以的话,你能帮我完成最后一个功能吗
class Kill_Tasks:
   def __init__(self):
      self.action = None
      self.cmd=[]
   def kill_tasks(self):
      self.action = False
      #exp task = chrome.exe
      target = 'chrome.exe'
      try:
         self.cmd = suprocess.run(['taskkill','/im', target,'/f'],capture_output=True, check=True)
         self.action = True
      except subprocess.CalledProcessError as e:
         print(e)
         self.action = False
      return self.cmd
   def action_required(self):
         tries = 3
         for i in range(tries):
             try:
                 self.kill_tasks()
                 self.action = True
             except subprocess.CalledProcessError as e:
                 self.action = False
                 if i < tries -1:
                  continue
                 else:
                  raise
            break

import unittest
from unittest.mock import patch
from subprocess import CompletedProcess, CalledProcessError
from kill_tasks import Kill_Tasks


class TestKillTasks(unittest.TestCase):
    @patch('kill_tasks.subprocess.run')
    def test_kill_tasks_success(self, mock_run):
        mock_run.return_value = CompletedProcess(args=['taskkill', '/im', 'chrome.exe', '/f'], returncode=0)
        kill_tasks_instance = Kill_Tasks()
        cmd = kill_tasks_instance.kill_tasks()
        mock_run.assert_called_once_with(['taskkill', '/im', 'chrome.exe', '/f'], capture_output=True, check=True)
        self.assertIsInstance(cmd, CompletedProcess)
        self.assertTrue(kill_tasks_instance.action)

    @patch('kill_tasks.subprocess.run')
    def test_kill_tasks_exception(self, mock_run):
        mock_run.side_effect = CalledProcessError(returncode=1, cmd='123')
        kill_tasks_instance = Kill_Tasks()
        kill_tasks_instance.kill_tasks()
        self.assertFalse(kill_tasks_instance.action)

    @patch('action_required.subprocess.run')
    def test_action_required_success(self, mock_run):
        mock_run.return_value = CompletedProcess(args=['taskkill', '/im', 'chrome.exe', '/f'], returncode=0)
        kill_tasks_instance = Kill_Tasks()
        cmd = kill_tasks_instance.action_required()
        mock_run.assert_called_once_with(['taskkill', '/im', 'chrome.exe', '/f'], capture_output=True, check=True)
        self.assertIsInstance(cmd, CompletedProcess)
        self.assertTrue(kill_tasks_instance.action)

```Need help for the function below:```
    @patch('action_required.subprocess.run')
    def test_action_required_exception(self, mock_run):
        mock_run.side_effect = CalledProcessError(returncode=1, cmd='false')
        kill_tasks_instance = Kill_Tasks()
        kill_tasks_instance.action_required()
        #it returns True, it should return False
        self.assertFalse(kill_tasks_instance.action)


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