[Tutor] unittest not working

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Nov 19 13:52:07 EST 2015


On 19 November 2015 at 13:25, Mike <python at thestraws.net> wrote:
> I'm trying to unit test a self-built regular expression processor for  an
> assignment. I'm trying to set up unit tests for the package, but it's not
> executing them. This is my first time trying to use the unittest module, so
> I'm sure I'm missing something, I'm just not sure what. I even put a test
> case in there I knew would fail just to try it.
>
> Unit Test code:
> import unittest
> from regex import regexp
>
> class RegexTest(unittest.TestCase):
>     def fail_test(self):
>         self.assertEqual(1, 2)
>
>     def basic_test(self):
>         self.assertEqual(regexp('Hello', 'Goodbye'), '')
>         self.assertEqual(regexp('hello', 'ello'), 'ello')
>         with self.assertRaises(SyntaxError):
>             regexp('hello', 'he)')
>
> if __name__ == '__main__':
>     unittest.main()
>
> Output:
>>>>
>
> ----------------------------------------------------------------------
> Ran 0 tests in 0.000s
>
> OK
> Exit code:  False

The unittest.main() call will attempt to find all of the TestCase
subclasses and for each class, create an instance and call all of the
methods that are named test_xxx. So if you rename your methods as
test_fail and test_basic then I think it should work.

The reason for this is that you might want to add other methods to
your TestCase subclass that will not be directly called by the test
runner but that you can use in your tests.

--
Oscar


More information about the Tutor mailing list