Python unit test wca
Generate unit tests for python code with watsonx code assistant¶
Introduction¶
In modern software development, writing python code is only the first step. Ensuring correctness, reliability and maintainability require thorough unit testing.
Python provides built-in tools such as the unittest module, allowing developers to import unittest and structure test.py files effectively. For complex or enterprise-level applications, writing unit tests manually can be time-consuming and prone to human error. Watsonx code assistant (WCA) further enhances this workflow by helping developers write unit tests automatically, creating unittest.TestCase classes and generating test suites to cover both normal and edge scenarios.
Why unit testing is essential?¶
Unit testing plays a critical role in validating python functions, methods and individual pieces of code by ensuring they behave as expected under different scenarios.
• Reliability:
Unit tests help detect failures early in the development cycle, preventing unexpected runtime errors. By validating expected outputs in advance, unit testing ensures predictable and stable application behaviour before code is deployed.
• Debugging:
Unit testing makes it easier to identify common runtime issues,
AssertionError occurs when a test’s expected outcome does not match the actual result returned by the code, typically indicating a logic or implementation issue.
TypeError occurs when an operation is performed on incompatible data types, such as passing a string where a numeric value is expected.
Other runtime exceptions (e.g., ValueError, IndexError) indicate invalid inputs or incorrect assumptions in the code.
By isolating these errors at the unit level, developers can quickly locate the source of the problem and correct it with minimal impact.
• Maintainability:
Unit tests enable safe refactoring by ensuring that existing functionality continues to work as intended. When changes are made, tests act as a safety net that helps prevent regressions and unintended side effects.
• Automation:
Unit testing supports automated execution using test runners and command-line tools, making it easy to integrate tests into CI/CD pipelines. This ensures continuous validation of code changes without manual intervention.
Consistent unit testing promotes reusable and well-structured code, standardized test modules and reliable CI/CD integration. Overall it reduces defects, improves Python code quality and enhances development productivity.
Demonstrating unit test generation using watsonx code assistant(WCA)¶
This section illustrates how watsonx code assistant (WCA) generates unit test code by analysing an existing python function.
Prompt given to WCA:
Generate unit tests using python unittest framework for the following function for normal cases, edge cases and error scenarios.
#Source code provided to WCA
def divide(a, b):
if b == 0:
raise ValueError("Division by zero is not allowed")
return a / b
#Code generated by WCA
import unittest
from calculator import divide
class TestDivide(unittest.TestCase):
def test_valid_division(self):
self.assertEqual(divide(10, 2), 5)
def test_division_result_float(self):
self.assertEqual(divide(7, 2), 3.5)
def test_division_by_zero(self):
with self.assertRaises(ValueError):
divide(5, 0)
# Required for jupyter to ignore kernel arguments
if __name__ == "__main__":
unittest.main(argv=['first-arg-is-ignored'], exit=False)
--------------------------------------------------------------------------- ImportError Traceback (most recent call last) Cell In[5], line 3 1 #Code generated by WCA 2 import unittest ----> 3 from test import divide 5 class TestDivide(unittest.TestCase): 7 def test_valid_division(self): ImportError: cannot import name 'divide' from 'test' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/test/__init__.py)
Prerequisites¶
Before we begin using watsonx code assistant (WCA), ensure that the following prerequisites are completed.
Step 1: Install visual studio code
Watsonx code assistant is available as an extension for visual studio code.
• Install the latest stable version of visual studio code.
• Verify that visual studio code launches correctly on our system.
Step 2: Install python
For python development and unit test generation,
• Install python version 3.9 or later
• Verify the installation using python --version
• Ensure that pip is available
• Create and activate a python virtual environment
Step 3: Install the watsonx code assistant extension
• Open visual studio code.
• Go to view > extensions
• Search for watsonx code assistant
• Install the extension
Use this link for installation details and once installed, the extension is available within visual studio code https://w3.ibm.com/w3publisher/wca-ibm/download/vscode
Step 4: Choose a backend (Cloud or Local)
Before configuring watsonx code assistant, it is important to understand the differences between Cloud and Local backends.
Why choose Cloud vs Local?¶
• Cloud backend is recommended for individual users and quick setup, providing managed infrastructure and minimal configuration.
• Local backend is typically used in enterprise environments where data privacy, compliance, or internal infrastructure requirements apply.
4.1 Cloud backend
• Uses a managed backend service
• Requires minimal setup
• Suitable for external users and personal development
• Automatically handles model updates and scaling
4.2 Local backend
• Runs within local or enterprise-managed infrastructure
• Backend configuration is managed by the organization
• Commonly used in regulated or security-sensitive environments
After selecting the desired backend, restart visual studio code to ensure the configuration is applied successfully.
Step 5: Configure watsonx code assistant
• Open visual studio code.
• Navigate to File > Preferences > Settings > Extensions > Watsonx code assistant
• Select the appropriate backend (Cloud or Local)
• Save the settings
• Restart visual studio code.
Step 6: Create or open a project
• Open an existing project or create a new project folder
• Ensure Python source files (.py) are visible in the workspace
• Activate a virtual environment if applicable.
Step 7: Verify installation
• Open a python file in visual studio code
• Select a function or class
• Use watsonx code assistant features such as unit test generation or code explanation
If the assistant responds successfully, the setup is complete.
Note for enterprise readers: performance and data privacy¶
• Performance: Cloud backends offer scalable performance with minimal setup, while local backends depend on internal infrastructure capacity.
• Data privacy: Local backends help ensure that source code and prompts remain within organizational boundaries, making them suitable for compliance-driven environments.
!python --version
Python 3.11.5
!pip --version
pip 25.3 from /Users/nivethasuruliraj/Documents/.venv/lib/python3.11/site-packages/pip (python 3.11)
Why watsonx code assistant is available in .py files but not in jupyter notebooks?¶
Watsonx code assistant (WCA) relies on vs code editor APIs to provide context aware features such as code explanation, refactoring and unit test generation. These APIs are available for standard source code files like .py, .java and .js.
Jupyter notebooks (.ipynb) use a kernel-based execution model and a notebook-specific interface that does not expose the same editor-level APIs. Because of this architectural difference, WCA cannot attach to or operate within individual notebook cells. As a result, the assistant is not visible in .ipynb files and this behaviour is expected.
Developer tip:¶
For data science workflows, use WCA in .py files to generate or modify code and unit tests, then execute or validate that logic interactively in jupyter notebooks.
How WCA generates unit tests?¶
Watsonx code assistant (WCA) automates the creation of python unit tests. The generated tests are executed using python’s unittest framework, providing a clear separation between test generation and test execution within the testing lifecycle.
Step-by-step workflow to generate and run unit tests
Step 1: Open a python source file
Open a .py file in VS code that contains one or more python functions (for example, add() or divide()).
Step 2: Select the target function
Highlight the function for which you want to generate unit tests.
Step 3: Generate unit tests using WCA
From the editor or command Palette, select Watsonx code assistant → Generate unit tests.
Step 4: Review the generated test file
WCA analyses the selected function and generates a test file (for example, test_calculator.py) that includes,
• A unittest.TestCase class
• Multiple test methods
• Assertions for normal, edge and error scenarios
No manual test creation is required.
Step 5: Run the generated tests
Run the generated tests using python’s unittest framework using python -m unittest. Python is responsible for executing the tests, while WCA is responsible for generating them.
Key components of python unit testing¶
1. Functions
Functions are the core building blocks of python code. Each function should be validated with unit tests to ensure it produces the correct output for valid inputs, handles edge cases and raises appropriate exceptions when necessary. Testing functions is essential in both small scripts and large codebases, where multiple pieces of code interact.
2. Testcase class
A testcase class is a subclass of unittest.Testcase and serves to group related test methods. Testcase classes help organize test code, making it readable, maintainable and modular. Each test method within a testcase class typically tests a single function or a specific scenario.
3. Assertions
Assertions are used to verify that code behaves as expected. Python’s unittest module provides multiple assert methods:
• assertequal: Verifies equality between expected and actual values.
• assertfalse: Ensures a condition is false.
• assertin: Checks whether an element exists within a collection.
• assertis: Confirms that two objects are the same instance.
• assertraises: Ensures a specific exception is raised, useful for error handling and validation of typeerror or assertionerror scenarios.
Well-crafted assert statements allow developers to catch failed tests early and improve the reliability of the python codebase.
4. Test fixtures
Test fixtures manage setup and teardown routines before and after test runs,
• setupclass: Prepares resources needed for all tests in a testcase class.
• teardown: Cleans up resources after each test or after the entire suite runs.
Fixtures ensure tests run in a controlled and consistent environment, preventing side effects between test functions and reducing debugging complexity.
5. Test suite and Test runner
A test suite is a collection of testcase classes or individual test functions, organized to run together. The test runner executes the suite, producing a verbose output to highlight test passes and failed tests, including detailed traceback logs.
• unittest.main: Provides a simple command-line interface to run test suites automatically.
• Test discovery: Automatically finds test.py files and test functions across the codebase, streamlining unit testing for large projects.
• Using test suites and test runners, developers can maintain comprehensive test coverage for all functions, subclasses and pieces of code, ensuring correctness across the python codebase.
import unittest
from Unit_test_WCA.calculator import add, divide
# 1. Functions example
class TestFunctions(unittest.TestCase):
def test_add_function(self):
self.assertEqual(add(2, 3), 5)
def test_divide_function(self):
self.assertEqual(divide(10, 2), 5)
with self.assertRaises(ValueError):
divide(10, 0)
# 2. Testcase class example
class TestMath(unittest.TestCase):
def test_basic(self):
self.assertTrue(10 > 5)
# 3. Assertions examples
class TestAssertions(unittest.TestCase):
def test_assertions(self):
self.assertEqual(add(2, 2), 4)
self.assertFalse(False)
self.assertIn(3, [1, 2, 3])
self.assertIs(None, None)
with self.assertRaises(ValueError):
divide(10, 0)
# 4. Test fixtures example
class TestUser(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.sample_data = {"name": "John", "age": 30}
def test_name(self):
self.assertEqual(self.sample_data["name"], "John")
@classmethod
def tearDownClass(cls):
cls.sample_data = None
# 5. Test suite and runner example
class TestSuiteExample(unittest.TestCase):
def test_add_suite(self):
self.assertEqual(add(5, 5), 10)
def suite():
suite = unittest.TestSuite()
suite.addTest(TestFunctions("test_add_function"))
suite.addTest(TestFunctions("test_divide_function"))
suite.addTest(TestMath("test_basic"))
suite.addTest(TestAssertions("test_assertions"))
suite.addTest(TestUser("test_name"))
suite.addTest(TestSuiteExample("test_add_suite"))
return suite
if __name__ == "__main__":
runner = unittest.TextTestRunner()
runner.run(suite())
...... ---------------------------------------------------------------------- Ran 6 tests in 0.004s OK
Python testing frameworks¶
The two most commonly used frameworks are unittest and pytest, each suited to different development needs.
1. unittest module
The unittest module is python’s built-in framework for unit testing and is part of the standard library.
Key features include:
• Creation of unittest.TestCase classes
• Organization of test methods in test files
• Automated test execution using unittest.main()
• Built-in assertion methods for validating return values and exception handling
When to use unittest:
unittest is well suited for enterprise and large-scale projects that require a structured, standardized, and widely supported testing approach. It provides a clear separation between production code and test code, making it easier to maintain and govern large python codebases.
2. pytest framework
pytest is a popular third-party testing framework known for its simplicity and flexibility.
Key features include:
• Lightweight setup with minimal boilerplate
• Automatic discovery of test files and test functions
• Support for plugins, decorators, and detailed output
• Effective for testing shared logic across multiple modules
When to use pytest:
pytest is ideal for rapid prototyping, data-driven testing, and experimentation, particularly in data science and analytics workflows where fast iteration and parameterized tests are common.
Summary:
Choosing the right framework
• Use unittest for enterprise-grade, structured and compliance-driven projects
• Use pytest for rapid development, experimentation and data-centric testing scenarios
Automating unit test generation with watsonx code assistant¶
Watsonx code assistant (WCA) provides a major boost in efficiency for writing unit tests:
• Analyses the python code and identifies pieces of code suitable for testing.
• Automatically generates unittest.TestCase subclasses, test functions and assert statements.
• Builds comprehensive test suites covering normal, edge and error scenarios.
• Supports both unittest and pytest frameworks for flexibility.
• Reduces debugging time and ensures higher validation coverage for our source code.
Automation with WCA ensures that even large codebases are systematically tested without requiring manual effort, allowing developers to focus on software development rather than writing repetitive tests.
Best practices for unit testing¶
Organize test.py files logically, reflecting the codebase structure.
Write clear and descriptive testcase classes for readability.
Cover both positive and negative scenarios, including typeerror and assertionerror cases.
Use setupclass and teardown routines for reusable test fixtures.
Run tests automatically using unittest.main or test runners from the CLI.
Integrate tests with github to automate CI/CD pipelines.
Regularly refactor test modules to maintain quality and reliability.
Use assert methods strategically to catch subtle bugs in pieces of code.
Leverage decorators in pytest for parameterized or conditional tests.
Following these best practices ensures that the python testing workflow is reliable, repeatable and maintainable across teams and projects.
Debugging and validation¶
Even with automation, understanding debugging is essential for high-quality python code,
• Utilize verbose output to monitor failed tests.
• Examine traceback logs to pinpoint the origin of exceptions.
• Apply assert statements consistently to validate return values and function behaviours.
• Ensure pieces of code reused across py files are adequately tested.
• Run test discovery regularly to catch untested functions or modules.
Proper debugging complements unit testing, helping maintain reliability across python projects.
Advanced unit testing concepts¶
• Test discovery: Automatically detects test.py files and test functions across large codebases.
• Subclasses: Advanced functions can be tested in unittest classes using subclassing techniques.
• Decorators: Parametrize or skip test methods conditionally.
• Plugins: Extend pytest for coverage, reporting or mocking external dependencies.
• Cross-platform testing: Run unit tests on linux, windows and cloud environments.
• GitHub integration: Trigger test runs automatically on commits and pull requests.
• Incremental testing: Continuously write unit tests as new functions and pieces of code are added.
These advanced strategies enhance coverage, reduce failed tests, simplify debugging, ensuring robust and scalable software development practices.
Unit testing use cases¶
Unit tests can be applied in diverse scenarios:
• Function validation: Ensuring functions return expected results for all inputs.
• Error handling: Capturing assertionerror, typeerror and other exceptions.
• API testing: Validating endpoints in data science or enterprise APIs.
• Refactoring: Ensuring unittest classes detect regressions during source code refactors.
• Edge cases: Testing subclasses, decorators or complex python functions.
• Automation: Running test suites through unittest.main or test runners to validate large codebases.
Automated unit test generation using WCA ensures comprehensive coverage and reduces manual effort in all these use cases.
Integrating unit tests in CI/CD pipelines¶
Integrating unit tests into CI/CD pipelines (for example, with GitHub) ensures that every test file, test case and test method is automatically validated as part of the development workflow.
Key benefits include,
• Commit and push test files to the source control repository
• Automatically trigger test runners on commits or pull requests
• Monitor test execution results and verbose output for failures
• Maintain test suites that cover functions, classes and subclasses
• Reduce manual debugging and improve overall python code quality across distributed teams
This integration transforms unit testing from a manual activity into an automated, repeatable and scalable process.
Enterprise compliance and governance considerations¶
For enterprise environments, integrating unit tests into CI/CD pipelines also supports,
• Auditability: Test execution results are logged and traceable for compliance reviews
• Consistency: Standardized test execution ensures uniform quality checks across teams
• Policy enforcement: Pipelines can enforce quality gates, preventing code merges when tests fail
• Security and reliability: Early detection of defects reduces production risk and supports regulatory requirements
By embedding unit testing into CI/CD pipelines, organizations can align software quality practices with enterprise governance, compliance and operational standards.
Conclusion¶
Watsonx code assistant empowers developers to streamline and standardize python unit testing by enabling the following:
• Efficient generation of unit tests for functions, classes, and subclasses
• Automated creation of unittest.TestCase classes in test files
• Comprehensive test coverage for normal scenarios, edge cases and exceptions
• Seamless integration of unit tests with GitHub and CI/CD pipelines
• Reduced debugging effort and improved overall Python code quality
By leveraging WCA, developers can focus more on application logic while maintaining reliable, maintainable, scalable test suites across both small and enterprise projects.
Developer tips and best practices¶
• Keep unit tests in a separate tests/ directory to improve maintainability and project structure.
• Use WCA to quickly bootstrap unit tests, then refine assertions manually to reflect business logic and domain-specific rules.
• Run unit tests locally before pushing changes to ensure faster feedback and cleaner CI/CD runs.