1/14
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
concept testing
executing code with intentional inputs to verify its behavior matches its design
asserts
python keywords used to conduct sanity checks; if evaluated to false, python raises an AssertionError and terminates execution
syntax of an assert
assert [condition], “optional error message”
Unit Testing
automated tool built into python that allows you to test your code systematically without the need to verify your code using print statements; needs to be imported as import unittest and inherits properties from the parent class unittest.TestCase
how to write a test class
import unittest, inheritance: class [class name](unittest.TestCase):, every test has to start with def test_[something] otherwise it’ll be ignored by framework
test coverage
a percentage that tells you how much of your source code is executed during testing; passing unit tests is only good if your test coverage is high; if low it means you’re never checking majority of your code
statement coverage
measures whether a specific line of code was executed
branch coverage
measures whether every possible branch/choice in a conditional statement was tested(ie passing a true value for an if statement is 50% branch coverage because you didn’t also pass a false statement)
which is a better indicator of bug-free code, statement coverage or branch coverage?
branch coverage, its stricter
how can you increase your test coverage metric?
write more unit tests with different input arguments that target else/elif blocks or exception handlers
purpose of testing
to prevent regression(mistakes made when changing code in one part breaks another component)
purpose of asserts vs unittest
asserts are used to catch violations inside source code, unittest is used to externally validate APIs and function edge cases
what unittest statement checks when 2 things are equal
self.assertEqual(actual, expected)
what unittest checks if a statement evaluates to True
self.assertTrue(condition)
what unittest allows for code to throw an error instead of crashing silently
with self.assertRaises(ExceptionType)