Python测试类卡在';实例化测试';

Python测试类卡在';实例化测试';,python,matplotlib,Python,Matplotlib,我的测试类生成所需的绘图,但每次我都必须手动停止执行-控制台继续显示“实例化测试”;有人能看出为什么死刑从未停止过吗?任何关于增加我的代码“pythonicness”的提示都将不胜感激 (在Mac OS X 10.11.6上的PyCharm CE 2016.2.1中运行的Python 3.5) #测试各种排序算法的程序。它生成各种大小的随机列表, #对它们进行排序,然后测试: #a。列表按升序排列 #b。元素集与原始列表相同 #c。记录所用的时间 随机输入 导入时间信息 从unittest导入T

我的测试类生成所需的绘图,但每次我都必须手动停止执行-控制台继续显示“实例化测试”;有人能看出为什么死刑从未停止过吗?任何关于增加我的代码“pythonicness”的提示都将不胜感激

(在Mac OS X 10.11.6上的PyCharm CE 2016.2.1中运行的Python 3.5)

#测试各种排序算法的程序。它生成各种大小的随机列表,
#对它们进行排序,然后测试:
#a。列表按升序排列
#b。元素集与原始列表相同
#c。记录所用的时间
随机输入
导入时间信息
从unittest导入TestCase
将matplotlib.pyplot作为plt导入
从分拣机导入插入排序、合并排序、快速排序
类TestSort(TestCase):
def测试_分拣(自):
times_insertionsort=[]保存插入排序的运行时间
times_quicksort=[]保存快速排序的运行时间
times_mergesort=[]保存合并排序的运行时间
长度=[]#保存数组长度
#确定要创建的列表的数量
对于范围(0,13)内的i:
#初始化一个新的空列表
预排序=[]
#确定列表的长度,然后用“随机”数据填充列表
对于范围(0,i*100)内的j:
pre_sort.append(random.randint(0,1000))
#记录列表的长度
追加长度(len(pre_排序))
#记录快速排序对列表排序所用的时间
start\u time=timeit.default\u timer()
后快速排序=快速排序(前排序)
finish\u time=timeit.default\u timer()
times\u quicksort.append((完成时间-开始时间)*1000)
#记录insertionsort排序列表所用的时间
start\u time=timeit.default\u timer()
post_insertionsort=insertionsort(pre_排序)
finish\u time=timeit.default\u timer()
times\u insertionsort.append((完成时间-开始时间)*1000)
#记录mergesort对列表排序所用的时间
start\u time=timeit.default\u timer()
post\u mergesort=合并排序(pre\u排序)
finish\u time=timeit.default\u timer()
次数\u合并排序.append((完成时间-开始时间)*1000)
#检查:
#a。列表按升序排列
#b。元素集与原始列表相同
对于范围内的k(0,len(预排序)-1):
self.assertTrue(pre_排序中的post_insertionsort[k]

self.assertTrue(post_insertionsort[k]您需要关闭显示绘图的窗口,以便脚本(本例中的测试)继续/完成/完成,重点是:

当您想在显示器上查看绘图时,用户界面后端将需要启动GUI mainloop。这就是
show()
所做的。它告诉matplotlib提升迄今为止创建的所有图形窗口并启动mainloop。因为此mainloop在默认情况下处于阻塞状态(即,脚本执行暂停),您应该在每个脚本结束时只调用一次。脚本执行将在最后一个窗口关闭后恢复。因此,如果您使用matplotlib仅生成图像,而不需要用户界面窗口,则无需调用show(请参见在不显示窗口的情况下生成图像以及什么是后端?)


或者不要在测试中使用
show()
,只需生成一个映像,您可以稍后对其进行验证。

我在Django中遇到了相同的错误。我发现其中一个迁移未应用

# A program to test various sorting algorithms. It generates random lists of various sizes, 
# sorts them, and then tests that:
#   a. the list is in ascending order
#   b. the set of elements is the same as the original list
#   c. record the time taken

import random
import timeit
from unittest import TestCase

import matplotlib.pyplot as plt

from Sorter import insertionsort, mergesort, quicksort


class TestSort(TestCase):
    def test_sorting(self):

        times_insertionsort = []   # holds the running times for insertion sort
        times_quicksort = []       # holds the running times for quick sort
        times_mergesort = []       # holds the running times for merge sort
        lengths = []               # holds the array lengths

        # determine the number of lists to be created
        for i in range(0, 13):

            # initialise a new empty list
            pre_sort = []

            # determine the list's length, then populate the list with 'random' data
            for j in range(0, i * 100):
                pre_sort.append(random.randint(0, 1000))

            # record the length of the list
            lengths.append(len(pre_sort))

            # record the time taken by quicksort to sort the list
            start_time = timeit.default_timer()
            post_quicksort = quicksort(pre_sort)
            finish_time = timeit.default_timer()
            times_quicksort.append((finish_time - start_time) * 1000)

            # record the time taken by insertionsort to sort the list
            start_time = timeit.default_timer()
            post_insertionsort = insertionsort(pre_sort)
            finish_time = timeit.default_timer()
            times_insertionsort.append((finish_time - start_time) * 1000)

            # record the time taken by mergesort to sort the list
            start_time = timeit.default_timer()
            post_mergesort = mergesort(pre_sort)
            finish_time = timeit.default_timer()
            times_mergesort.append((finish_time - start_time) * 1000)

            # check that:
            #   a. the list is in ascending order
            #   b. the set of elements is the same as the original list
            for k in range(0, len(pre_sort) - 1):
                self.assertTrue(post_insertionsort[k] in pre_sort)
                self.assertTrue(post_insertionsort[k] <= post_insertionsort[k + 1])
                self.assertTrue(post_mergesort[k] == post_insertionsort[k])
                self.assertTrue(post_mergesort[k] == post_quicksort[k])

        # plot the results
        plt.plot(lengths, times_insertionsort, 'r')
        plt.plot(lengths, times_quicksort, 'g')
        plt.plot(lengths, times_mergesort, 'b')
        plt.xlabel('List size')
        plt.ylabel('Execution time (ms)')
        plt.show()