Python
 Computer >> コンピューター >  >> プログラミング >> Python

PythonでUnittestを使用したユニットテスト


このチュートリアルでは、ユニットテストについて学習します。 ユニットテストを使用する 組み込みモジュール。テストは、ソフトウェア開発において主要な役割を果たします。プロダクション自体に進む前に、問題を知っているでしょう。

unittest と呼ばれる組み込みモジュールを使用して、Pythonでのテストの基本を学びます。 。チュートリアルに飛び込みましょう。

ユニットテストとは何ですか?

例としてログインシステムを取り上げます。ログインフォームの各フィールドは、ユニット/コンポーネントです。そして、それらのユニット/コンポーネントの機能をテストすることは、ユニットテストとして知られています。

ユニットテストフレームワークの基本構造を見てみましょう。

# importing unittest module
import unittest
# unittest will test all the methods whose name starts with 'test'
class SampleTest(unittest.TestCase):
   # return True or False
   def test(self):
      self.assertTrue(True)
# running the test
unittest.main()

出力

上記のプログラムを実行すると、次の結果が得られます。

----------------------------------------------------------------------
Ran 1 test in 0.001s
OK

2。文字列メソッドのテスト

次に、サンプルのテストケースを使用してさまざまな文字列メソッドをテストします。メソッド名はtestで始まる必要があることに注意してください。

これから作成する各メソッドの簡単な紹介を見てみましょう。

  • test_string_equality

    • このメソッドは、 assertEqaul を使用して、2つの文字列が等しいかどうかをテストします unittest.TestCase。のメソッド

  • test_string_case

    • このメソッドは、 assertEqaul を使用して、2つの文字列のケースが等しいかどうかをテストします unittest.TestCase。のメソッド

  • test_is_string_upper

    • このメソッドは、 assertTrue を使用して、文字列が大文字であるかどうかをテストします およびassertFalse unittest.TestCaseのメソッド 。

# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):
   # string equal
   def test_string_equality(self):
      # if both arguments are equal then it's succes
      self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
   # comparing the two strings
   def test_string_case(self):
      # if both arguments are equal then it's succes
      self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
   # checking whether a string is upper or not
   def test_is_string_upper(self):
      # used to check whether the statement is True or False
      # the result of expression inside the **assertTrue** must be True to pass the test case
      # the result of expression inside the **assertFalse** must be False to pass the test case
      self.assertTrue('TUTORIALSPOINT'.isupper())
      self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main()
出力

上記のコードを実行すると、すべてのテストケースに合格すると、次の結果が得られます。

...
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK

次のプログラムを実行して、失敗したテストケースの出力を確認します。

# importing unittest module
import unittest
class TestingStringMethods(unittest.TestCase):

   # string equal
   def test_string_equality(self):
      # if both arguments are equal then it's succes
      self.assertEqual('ttp' * 5, 'ttpttpttpttpttp')
   # comparing the two strings
   def test_string_case(self):
      # if both arguments are equal then it's succes
      self.assertEqual('tutorialspoint'.upper(), 'TUTORIALSPOINT')
   # checking whether a string is upper or not
   def test_is_string_upper(self):
      # used to check whether the statement is True or False
      # the result of expression inside the **assertTrue** must be True to pass the test case
      # the result of expression inside the **assertFalse** must be False to pass the test case
      self.assertTrue('TUTORIALSPOINt'.isupper())
      self.assertFalse('TUTORIALSpoint'.isupper())
# running the tests
unittest.main()
出力

上記のプログラムを実行すると、次の結果が得られます。

======================================================================
FAIL: test_is_string_upper (__main__.TestingStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "p:/Python Work/Stopwatch/practice.py", line 21, in test_is_string_upper
self.assertTrue('TUTORIALSPOINt'.isupper())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 3 tests in 0.016s
FAILED (failures=1)

すべてのテストケースの中で1つのテストケースが失敗した場合でも、メッセージは失敗します。

結論

チュートリアルで疑問がある場合は、コメントセクションでそれらについて言及してください。


  1. Pythonを使用しているWhatsapp?

    このセクションでは、Whatsappチャットボットを作成しますが、TwitterやFacebook用の他のいくつかのチャットボットとは異なり、whatsappのポリシーのため、whatsappチャットボットはプラットフォーム上で直接実行されません。 しかし、Pythonの非常にスマートなパッケージであるseleniumを使用して、開発者がブラウザのアクティビティを自動化できるようにする方法があります。これにより、ブラウザからwhatsapp-webを利用できます。 要件 物事を成し遂げるためには、3つの基本的なことが必要です。セレン。 ターミナルで以下のコマンドを実行するだけで、pipを

  2. PythonでのCX_Freezeの使用

    時々私たちは非常にエキサイティングな何か違うものを作りたいと感じます、そして人間の性質によれば、私たちはいつもそれを共有するのが大好きです。 Pythonもそれらの願いを満たします。 Pythonを使用して、Pythonプログラムを友人と共有したい場合は、それを行うことができます。必要なのは、マシンのプログラムで使用されるすべてのモジュールに同じバージョンのPythonをインストールすることだけです。 まず、 pip install CX_Frezzeを使用してCX_Freezeモジュールをインストールする必要があります コマンドプロンプトのコマンド。 最初のステップは、この割り当て、