From charlesreid1

(Created page with "basic strategy for writing tests: use pytest methods and files named <code>test_</code> or <code>_test</code> will be searched for unit tests when you run pytest on the comma...")
 
No edit summary
 
Line 3: Line 3:
methods and files named <code>test_</code> or <code>_test</code> will be searched for unit tests when you run pytest on the command line.
methods and files named <code>test_</code> or <code>_test</code> will be searched for unit tests when you run pytest on the command line.


Basic code:
Basic example:


<pre>
<pre>
import unittest
import unittest


class MyTest(unittest.TestCase):
class TestStringMethods(unittest.TestCase):
    def __init__(self):
        pass
</pre>


setup
    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')


teardown
    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())


test1
    def test_split(self):
        s = 'hello world'
        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)


test2
if __name__ == '__main__':
    unittest.main()
</pre>

Latest revision as of 01:10, 1 February 2019

basic strategy for writing tests: use pytest

methods and files named test_ or _test will be searched for unit tests when you run pytest on the command line.

Basic example:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        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)

if __name__ == '__main__':
    unittest.main()