Can I use TDD (unittest module) for Blender addons creation?

Hello,

I want to use unittest module for writing addons for Blender by TDD.

I created this script:


import unittest


class Phonebook(unittest.TestCase):
    
    def test_create_phonebook(self):
        phonebook = Phonebook()

I need to run this script by this command: python -m unittest

I write this command in the blender console, but I see the error:

>>> python -m unittest
File “<blender_console>”, line 1
python -m unittest
^
SyntaxError: invalid syntax

I copy the script from here (see below) and click on “Run Script” button but the Blender is crashed.


import unittest


class TestStringMethods(unittest.TestCase):


    def test_upper(self):
        self.assertEqual('foof'.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()



I found the solution for my problem here. It runs from Blender by click on “Run Script” button


import unittest


class BlenderProjectTest(unittest.TestCase):
	def testDBConnection(self):
		print('I am in a test case')
		self.assert_(1)


#if __name__ == '__main__':
try:
	try:
		print('I am __main__ and I am about to run unittest.main()')
		unittest.main()
	except SystemExit:
		pass
	except:
		raise
except:
	print('Exception: ')
1 Like

If you run this script the Blender is closed and writes this message in the console:

Saved session recovery to ‘C:\Users\8Observer8\AppData\Local\Temp\quit.blend’
Error: Not freed memory blocks: 7, total unfreed memory 0.001068 MB


import unittest


class PhonebookTest(unittest.TestCase):
    def test_create_phonebook(self):
        self.assertTrue(True)


unittest.main()

But this code works correct and writes this message in the console:


Ran 1 test in 0.000s

OK


import unittest


class PhonebookTest(unittest.TestCase):
    def test_create_phonebook(self):
        self.assertTrue(True)


try:        
    unittest.main()
except SystemExit:
    pass
except:
    raise


Blender python is embedded inside the blender executable.

Also you do not need to use the python’s implementation of TDD you can create your own depending on your needs. In the end TDD is all about automated testes. Unintest offer some convenience object that will make your life easier but is not made with blender python in mind. For example unittest is made to work from the terminal but we blender python coders work mainly from inside the Blender GUI.

You can do some cool things with a bit of imagination you can for example create a collection of tests that run as a separate addon.

I will use built-in Blender Python Tools for TDD: unittest and unittest.mock

I found this very good video course: Unit Testing with Python | Pluralsight

There is also another TDD library that may interest you that is more powerful and flexible called, “nose”