Python 模仿一个我';m使用unittest进行测试

Python 模仿一个我';m使用unittest进行测试,python,unit-testing,python-3.x,Python,Unit Testing,Python 3.x,我有一个函数vertex\u set,它接受一个变量graph(它是一组边和一组顶点的元组)并返回顶点。该函数位于名为无向图的模块中,不在类中,而是一个常规函数 我正在编写一个单元测试,我的测试函数如下所示: def test_vertex_test(self, graph) 我使用的是Python3.3.0,它包含mock内置库。我会在这个测试中模拟graph变量:graph=MagicMock(),然后用这个图调用实函数吗?或者我只是构造一些图,然后传递它,然后调用assertEquals

我有一个函数
vertex\u set
,它接受一个变量
graph
(它是一组边和一组顶点的元组)并返回顶点。该函数位于名为
无向图
的模块中,不在类中,而是一个常规函数

我正在编写一个单元测试,我的测试函数如下所示:

def test_vertex_test(self, graph)
我使用的是Python3.3.0,它包含
mock
内置库。我会在这个测试中模拟
graph
变量:
graph=MagicMock()
,然后用这个图调用实函数吗?或者我只是构造一些图,然后传递它,然后调用
assertEquals
检查它是否返回了真实的东西


我该如何在这里使用嘲弄或存根?或者在我的例子中它是无关的?

正如您已经知道的,编写单元测试是为了确保您的代码按照预期和设计的方式运行。我们通过将期望的参数传递给函数并测试以获得期望的结果来实现这一点。相反,如果我们传递了意外的参数,我们希望测试我们的函数是否能够处理它们。因此,我们孤立地运行测试;每个测试方法应该只测试应用程序的一个特性

有时,我们需要测试的函数使用的库不是应用程序的一部分。这打破了隔离,使测试代码逻辑变得困难。例如,我们的方法调用一个外部库(它遍历文件系统,创建文件,读取其他文件,最后返回是否成功)。在这个极端的例子中,我们无法控制这个外部库的输出。这使得我们很难测试代码,因为我们不知道库将返回什么。输入Mocking。我们将模拟对这个第三方函数的调用,以返回我们所期望的,True和False。通过这种方式,我们可以测试我们的代码是否可以处理这两个结果,而不需要运行外部库的开销

现在,请回答您剩下的问题:)

如果您的
vertex\u集
未使用第三方库,则以下内容适用

为了解释如何测试它,我正在编写
vertex\u set
的工作原理

无向_图.py

def vertex_set(graph):
   """Set's the vertex based on the graph passed.

      Returns set of vertices.
      Raises ValueError if graph is not a tuple of sets.
    """
import unittest
import undirected_graph

class UndirectedGraphTestCase(unittest.testCase):
    def test_vertex_set(self):

        # Test we can set a vertex
        graph = ({1, 2}, {3, 4})
        result = undirected_graph.vertex_set(graph)
        self.assertEqual(result. {3, 4})

        # Test we can handle a negative vertex
        graph = ({1, 2}, {-3, -4})
        result = undirected_graph.vertex_set(graph)
        self.assertEqual(result, {-3, -4})

        # Test we can handle when we give an invalid graph
        graph = ()
        self.assertRaises(ValueError, undirected_graph.vertex_set, graph)
由于该
顶点集
可以返回两种不同的结果(顶点集和raise VALUERROR),我们的测试需要确保在适当的时候可以得到这些结果

test_无向图.py

def vertex_set(graph):
   """Set's the vertex based on the graph passed.

      Returns set of vertices.
      Raises ValueError if graph is not a tuple of sets.
    """
import unittest
import undirected_graph

class UndirectedGraphTestCase(unittest.testCase):
    def test_vertex_set(self):

        # Test we can set a vertex
        graph = ({1, 2}, {3, 4})
        result = undirected_graph.vertex_set(graph)
        self.assertEqual(result. {3, 4})

        # Test we can handle a negative vertex
        graph = ({1, 2}, {-3, -4})
        result = undirected_graph.vertex_set(graph)
        self.assertEqual(result, {-3, -4})

        # Test we can handle when we give an invalid graph
        graph = ()
        self.assertRaises(ValueError, undirected_graph.vertex_set, graph)

希望我能弄清楚什么时候需要模拟,什么时候不需要。如果您还有更多问题,请告诉我:)

正如您已经知道的,编写单元测试是为了确保您的代码按照预期和设计的方式运行。我们通过将期望的参数传递给函数并测试以获得期望的结果来实现这一点。相反,如果我们传递了意外的参数,我们希望测试我们的函数是否能够处理它们。因此,我们孤立地运行测试;每个测试方法应该只测试应用程序的一个特性

有时,我们需要测试的函数使用的库不是应用程序的一部分。这打破了隔离,使测试代码逻辑变得困难。例如,我们的方法调用一个外部库(它遍历文件系统,创建文件,读取其他文件,最后返回是否成功)。在这个极端的例子中,我们无法控制这个外部库的输出。这使得我们很难测试代码,因为我们不知道库将返回什么。输入Mocking。我们将模拟对这个第三方函数的调用,以返回我们所期望的,True和False。通过这种方式,我们可以测试我们的代码是否可以处理这两个结果,而不需要运行外部库的开销

现在,请回答您剩下的问题:)

如果您的
vertex\u集
未使用第三方库,则以下内容适用

为了解释如何测试它,我正在编写
vertex\u set
的工作原理

无向_图.py

def vertex_set(graph):
   """Set's the vertex based on the graph passed.

      Returns set of vertices.
      Raises ValueError if graph is not a tuple of sets.
    """
import unittest
import undirected_graph

class UndirectedGraphTestCase(unittest.testCase):
    def test_vertex_set(self):

        # Test we can set a vertex
        graph = ({1, 2}, {3, 4})
        result = undirected_graph.vertex_set(graph)
        self.assertEqual(result. {3, 4})

        # Test we can handle a negative vertex
        graph = ({1, 2}, {-3, -4})
        result = undirected_graph.vertex_set(graph)
        self.assertEqual(result, {-3, -4})

        # Test we can handle when we give an invalid graph
        graph = ()
        self.assertRaises(ValueError, undirected_graph.vertex_set, graph)
由于该
顶点集
可以返回两种不同的结果(顶点集和raise VALUERROR),我们的测试需要确保在适当的时候可以得到这些结果

test_无向图.py

def vertex_set(graph):
   """Set's the vertex based on the graph passed.

      Returns set of vertices.
      Raises ValueError if graph is not a tuple of sets.
    """
import unittest
import undirected_graph

class UndirectedGraphTestCase(unittest.testCase):
    def test_vertex_set(self):

        # Test we can set a vertex
        graph = ({1, 2}, {3, 4})
        result = undirected_graph.vertex_set(graph)
        self.assertEqual(result. {3, 4})

        # Test we can handle a negative vertex
        graph = ({1, 2}, {-3, -4})
        result = undirected_graph.vertex_set(graph)
        self.assertEqual(result, {-3, -4})

        # Test we can handle when we give an invalid graph
        graph = ()
        self.assertRaises(ValueError, undirected_graph.vertex_set, graph)
希望我能弄清楚什么时候需要模拟,什么时候不需要。如果您有更多问题,请告诉我:)