Why They Matter
As companies get bigger, so do programs. And they become giant beasts that become more and more difficult to tame.
Lucklily there are methods in place that allow you to test your code before deploying.
test.py is the typical file name one would use to create a test environment.
Pylint and Pep 8 are really helpful in debugging code. However, ultimately you would want to test before deployment.
Unittest
The example Andre uses involves a class and importing the main.py or the file name in question is important.
`import unittest
import main
class TestMain(unittest.TestCase):
def test_do_stuff(self):
num == 10
result = main.do_stuff(test_param)
self.assertEqual(result, 15)
def test_do_stuff2(self):
test_param = 'A string!'
result = main.do_stuff(test_param)
self.assertIsinstance(result, TypeError)
unittest.main()`
The first function tests for the correct answer. In this case, the function is looking to see if the code it is testing will output 15. This will log errors when run.
When you run some of these scripts, you can even customize the messages you leave for someone else testing them.
You would write them like this.
`class TestMain(unittest.TestCase):
def setUp(self):
print('Testing function now!')
def test_do_stuff(self):
num == 10
result = main.do_stuff(test_param)
self.assertEqual(result, 15)`
By adding the setUp function, it allows you to print messages before every test has begun.
In the command line, to run all of your tests at once, you type python3 -m unittest.
On a Mac, you can type pwd to figure out your present working directory, and then type touch to create a new file within that directory. ls to list the files in that directory. cd followed by the directory name, to move to that directory.
To see your code in action, simply type python3 -m unittest -v for running your tests in verbose mode. This allows you to see any messages you might leave behind for other devs working on your code. Especially if you leave behind ways of testing.