Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
bc1fda39
Kaydet (Commit)
bc1fda39
authored
Mar 24, 2015
tarafından
Ezio Melotti
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
#11468: improve unittest basic example. Initial patch by Florian Preinstorfer.
üst
f689f104
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
38 deletions
+30
-38
unittest.rst
Doc/library/unittest.rst
+29
-38
ACKS
Misc/ACKS
+1
-0
No files found.
Doc/library/unittest.rst
Dosyayı görüntüle @
bc1fda39
...
@@ -109,37 +109,29 @@ The :mod:`unittest` module provides a rich set of tools for constructing and
...
@@ -109,37 +109,29 @@ The :mod:`unittest` module provides a rich set of tools for constructing and
running tests. This section demonstrates that a small subset of the tools
running tests. This section demonstrates that a small subset of the tools
suffice to meet the needs of most users.
suffice to meet the needs of most users.
Here is a short script to test three
functions from the :mod:`random` module
::
Here is a short script to test three
string methods
::
import random
import unittest
import unittest
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
class TestStringMethods(unittest.TestCase):
self.seq = range(10)
def test_shuffle(self):
def test_upper(self):
# make sure the shuffled sequence does not lose any elements
self.assertEqual('foo'.upper(), 'FOO')
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))
# should raise an exception for an immutable sequence
def test_isupper(self):
self.assertRaises(TypeError, random.shuffle, (1,2,3))
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_choice(self):
def test_split(self):
element = random.choice(self.seq)
s = 'hello world'
self.assertTrue(element in self.seq)
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
def test_sample(self):
if __name__ == '__main__':
with self.assertRaises(ValueError):
unittest.main()
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
if __name__ == '__main__':
unittest.main()
A testcase is created by subclassing :class:`unittest.TestCase`. The three
A testcase is created by subclassing :class:`unittest.TestCase`. The three
individual tests are defined with methods whose names start with the letters
individual tests are defined with methods whose names start with the letters
...
@@ -147,16 +139,15 @@ individual tests are defined with methods whose names start with the letters
...
@@ -147,16 +139,15 @@ individual tests are defined with methods whose names start with the letters
represent tests.
represent tests.
The crux of each test is a call to :meth:`~TestCase.assertEqual` to check for an
The crux of each test is a call to :meth:`~TestCase.assertEqual` to check for an
expected result; :meth:`~TestCase.assertTrue` to verify a condition; or
expected result; :meth:`~TestCase.assertTrue` or :meth:`~TestCase.assertFalse`
:meth:`~TestCase.assertRaises` to verify that an expected exception gets raised.
to verify a condition; or :meth:`~TestCase.assertRaises` to verify that a
These methods are used instead of the :keyword:`assert` statement so the test
specific exception gets raised. These methods are used instead of the
runner can accumulate all test results and produce a report.
:keyword:`assert` statement so the test runner can accumulate all test results
and produce a report.
When a :meth:`~TestCase.setUp` method is defined, the test runner will run that
The :meth:`~TestCase.setUp` and :meth:`~TestCase.tearDown` methods allow you
method prior to each test. Likewise, if a :meth:`~TestCase.tearDown` method is
to define instructions that will be executed before and after each test method.
defined, the test runner will invoke that method after each test. In the
They are covered in more details in the section :ref:`organizing-tests`.
example, :meth:`~TestCase.setUp` was used to create a fresh sequence for each
test.
The final block shows a simple way to run the tests. :func:`unittest.main`
The final block shows a simple way to run the tests. :func:`unittest.main`
provides a command-line interface to the test script. When run from the command
provides a command-line interface to the test script. When run from the command
...
@@ -172,18 +163,18 @@ Instead of :func:`unittest.main`, there are other ways to run the tests with a
...
@@ -172,18 +163,18 @@ Instead of :func:`unittest.main`, there are other ways to run the tests with a
finer level of control, less terse output, and no requirement to be run from the
finer level of control, less terse output, and no requirement to be run from the
command line. For example, the last two lines may be replaced with::
command line. For example, the last two lines may be replaced with::
suite = unittest.TestLoader().loadTestsFromTestCase(TestS
equenceFunction
s)
suite = unittest.TestLoader().loadTestsFromTestCase(TestS
tringMethod
s)
unittest.TextTestRunner(verbosity=2).run(suite)
unittest.TextTestRunner(verbosity=2).run(suite)
Running the revised script from the interpreter or another script produces the
Running the revised script from the interpreter or another script produces the
following output::
following output::
test_
choice (__main__.TestSequenceFunction
s) ... ok
test_
isupper (__main__.TestStringMethod
s) ... ok
test_s
ample (__main__.TestSequenceFunction
s) ... ok
test_s
plit (__main__.TestStringMethod
s) ... ok
test_
shuffle (__main__.TestSequenceFunction
s) ... ok
test_
upper (__main__.TestStringMethod
s) ... ok
----------------------------------------------------------------------
----------------------------------------------------------------------
Ran 3 tests in 0.
110
s
Ran 3 tests in 0.
001
s
OK
OK
...
...
Misc/ACKS
Dosyayı görüntüle @
bc1fda39
...
@@ -1077,6 +1077,7 @@ Claudiu Popa
...
@@ -1077,6 +1077,7 @@ Claudiu Popa
John Popplewell
John Popplewell
Davin Potts
Davin Potts
Guillaume Pratte
Guillaume Pratte
Florian Preinstorfer
Amrit Prem
Amrit Prem
Paul Prescod
Paul Prescod
Donovan Preston
Donovan Preston
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment