其他班的单元测试班开始

时间:2019-03-20 13:24:47

标签: python unit-testing python-unittest

我正在尝试测试可启动其他课程的课程。这是课程:

from crit.executors.result import Status

from crit.exceptions import SingleExecutorFailedException
from crit.executors.git import GitCloneExecutor, GitCheckoutExecutor, GitPullExecutor

from crit.config import Host
from crit.executors import MultiExecutor, Result


class GitExecutor(MultiExecutor):
    """
    Executor that clones, checks out and pulls a repository

    Args:
        repository (str): The repository that will be cloned. :obj:`required`
        path (str): Path to which the repository should be cloned. :obj:`required`
        version (str): Version of the repository. Defaults to :obj:`'master'`
        force (bool): Force changes made in the repository to be discarded. Defaults to :obj:`False`
        **kwargs: Everything that will be passed to the executors. :obj:`optional`
    """

    repository: str
    path: str
    version: str
    force: bool
    kwargs: dict

    def __init__(self, repository: str, path: str, version: str = 'master', force: bool = False, **kwargs):
        self.repository = repository
        self.path = path
        self.version = version
        self.force = force
        self.kwargs = kwargs

    def execute(self, host: Host, **kwargs) -> Result:
        """

        Args:
            host (Host): host on which the executor is ran
            **kwargs (dict): The extra vars passed in the function

        Returns:
            Result from the git executors
        """

        try:
            results = []

            results.append(GitCloneExecutor(
                repository=self.repository,
                name=f'Cloning {self.repository}',
                chdir=self.path,
                **self.kwargs
            ).execute(True))

            results.append(GitCheckoutExecutor(version=self.version, force=self.force, chdir=self.path,
                                name=f'Checking out {self.version} for {self.repository}', **self.kwargs)
                           .execute(True))

            results.append(GitPullExecutor(force=self.force, chdir=self.path, name=f'Pulling {self.repository}',
                                           **self.kwargs).execute(True))

            return self.result_from_executor(results, 'Updated github repository')
        except SingleExecutorFailedException:
            return Result(Status.FAIL, message='An executor failed')

我想测试此类是否以正确的方式调用其他类的构造函数。但是这样做有些麻烦。

这是我的测试

import unittest
from unittest import mock
from unittest.mock import Mock
from crit.config import Localhost
from crit.executors.git import GitExecutor, GitCloneExecutor


class ExecuteTest(unittest.TestCase):
    @mock.patch('crit.executors.git.GitCloneExecutor')
    @mock.patch('crit.executors.git.GitCheckoutExecutor')
    @mock.patch('crit.executors.git.GitPullExecutor')
    def test_command(self, clone, checkout, pull):
        repository = 'git@github.com:jessielaf/effe'
        version = 'develop'
        force = True
        chdir = '/test'

        clone.return_value = Mock()
        checkout.return_value = Mock()
        pull.return_value = Mock()

        executor = GitExecutor(
            repository=repository,
            version=version,
            force=force,
            chdir=chdir,
            host=Localhost()
        )

        executor.execute()

        clone.called_with(
            repository=repository,
            name=f'Cloning {repository}',
            **executor.get_base_attributes()
        )

        checkout.called_with(
            version=version,
            force=force,
            name=f'Checking out {version} for {repository}',
            **executor.get_base_attributes()
        )

        pull.called_with(
            force=force,
            name=f'Pulling {repository}',
            **executor.get_base_attributes()
        )

即使我放置了return_value Mock(),类仍然是初始化的,仍然调用了模拟类的execute函数。我也尝试模拟该功能,但最终还是遇到了sa

0 个答案:

没有答案