前提・実現したいこと
python: 3.6.4
公式ドキュメントの「10.11.品質管理」を学習しています。
公式:https://docs.python.jp/3/tutorial/stdlib.html#string-pattern-matching
定義済みの関数average()に対して、NameErrorが出ます。
解決法が見つからずお手上げです。
ご教示よろしくお願い致します。
発生している問題・エラーメッセージ
>python -m unittest test E ====================================================================== ERROR: test_average (test.TestStatisticalFunctions) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\study\python\official_document\10.standard _library\test.py", line 6, in test_average self.assertEqual(average([20, 30, 70]), 40.0) NameError: name 'average' is not defined ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (errors=1)
該当のソースコード
Python
1import unittest 2 3class TestStatisticalFunctions(unittest.TestCase): 4 5 def test_average(self): 6 self.assertEqual(average([20, 30, 70]), 40.0) 7 self.assertEqual(round(average([1, 5, 7]), 1), 4.3) 8 with self.assertRaises(ZeroDivisionError): 9 average([]) 10 with self.assertRaises(TypeError): 11 average(20, 30, 70) 12 13 def average(values): 14 """Computes the arithmetic mean of a list of numbers. 15 16 >>> print(average([20, 30, 70])) 17 40.0 18 """ 19 return sum(values) / len(values) 20 21if __name__ == "__main__": 22 unittest.main() # Calling from the command line invokes all tests
考えたこと
average()関数が、test_average()関数内から見えていないのかと思いましたが、
average()関数はクラスTestStatisticalFunctionsのローカル名前空間に入っているので、その可能性はないと予想しています。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/12 04:52