2016-08-13 209 views
1

我的测试类产生所需的图,但我必须每次都手动停止执行 - 控制台继续显示'实例化测试';任何人都可以发现为什么执行永不停止吗?任何提高我的代码的'Pythonic-ness'的提示也将不胜感激!Python测试类卡在'实例化测试'

(Python的3.5 PyCharm CE 2016年2月1日在Mac OS X上运行10.11.6)

# 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() 

回答

1

您需要关闭其中的情节显示,以使脚本(测试这个窗口案件)继续/完成/完成。 From the docs,重点是我的:

当你想在你的显示器上查看你的图,用户界面的后端将需要启动GUI主循环。这就是show()所做的。它告诉matplotlib提高到目前为止创建的所有数字窗口并启动主循环。 由于默认情况下此主循环处于阻塞状态(即脚本执行已暂停),因此每个脚本最后只能调用一次。脚本执行在最后一个窗口关闭后恢复。因此,如果您使用matplotlib仅生成图像并且不想要用户界面窗口,则不需要调用show(请参阅生成图像,而不会出现窗口,什么是后端?)。

或者不要使用show()进行测试,只是生成一个图像,您可以稍后再验证。