2016-08-13 6 views
1

私のテストクラスは必要なプロットを生成しますが、コンソールは引き続き実行を手動で停止する必要があります。実行が決して止まらない理由を誰にも見せてくれる?私のコードのPythonic-nessを増やすためのヒントもありがとう!Pythonテストクラスが「インスタンス化テスト」に固執しました

(Pythonの3.5は、Mac OS X 10.11.6にPyCharm CE 2016年2月1日に実行されている)

# 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にこれまでに作成されたFigureウィンドウのすべてを生成してメインループを開始するように指示します。 このメインループはデフォルトでブロックされているため(スクリプトの実行は一時停止しています)、スクリプトごとに一度だけ呼び出す必要があります。スクリプトの実行は、最後のウィンドウが閉じられた後に再開されます。したがって、matplotlibを使用してイメージのみを生成し、ユーザーインターフェイスウィンドウを必要としない場合は、showを呼び出す必要はありません(ウィンドウが表示されずにイメージを生成し、バックエンドとは?を参照してください)。

テスト用にshow()を使用せず、後で確認できる画像を生成するだけです。

関連する問題