166. Unit Testing with nose2
Snippet 1: Basic Test Case with nose2
import unittest
class TestBasic(unittest.TestCase):
def test_addition(self):
self.assertEqual(1 + 1, 2)
# Run using nose2
# Command: nose2Snippet 2: Testing a Function
import unittest
def add(a, b):
return a + b
class TestMathOperations(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
self.assertEqual(add(-1, 1), 0)
# Run using nose2Snippet 3: Test Case with Setup and Teardown
Snippet 4: Testing Exceptions
Snippet 5: Skipping Tests
Snippet 6: Parameterized Tests Using unittest.TestCase
Snippet 7: Grouping Tests in a Test Suite
Snippet 8: Mocking in Tests
Snippet 9: Asserting on Lists
Snippet 10: Testing File Operations
Last updated