vl - unit testing
Introduction to Unit Testing
Overview:
One of the most important yet often overlooked topics in software development.
Requires a different mindset from typical programming practices.
Increasing emphasis in organizations on unit testing.
Definition of Unit Testing
What is Unit Testing?
A method to ensure that code functions as intended.
Involves testing small, specific segments of code.
Differentiation from previous schooling methods, where code was tested manually by running the entire application.
Traditional method is slow, as it relies on having all components functioning.
Efficiency of Unit Testing:
Allows testing of isolated functions without needing to deploy the entire application.
Unit Testing Process
Creating Unit Tests:
Aim for specificity and granularity in tests.
Focus on specific classes and functions rather than the entire application.
Example:
If having a calculator class with an addition function, the unit test should focus solely on that addition function.
Clarifications on Unit Testing Scope
Limitations of Unit Testing:
Not for validating user input (despite the use of assertions).
Execution Timing:
Unit tests are run at compile time, not runtime.
Best Practices in Unit Testing
New Code and Corresponding Tests:
When writing new code, create corresponding unit tests to verify correct functionality.
Existing Tests:
Run the entire suite of existing tests after introducing new code to ensure nothing is broken.
Impact on Code Quality:
Increases confidence in code integrity and guarantees previous functionalities remain intact.
Unit Testing Libraries
Available Libraries:
Popular libraries for unit testing include:
Jasmine
Mocha
Karma
Acknowledgement of their existence is important; further learning about these libraries is encouraged.
Structure of Writing Unit Tests
Basic Structure:
Begin with writing a test suite to encapsulate all tests.
A variable pointing to a function containing sub-functions for individual tests.
Example naming convention: use the prefix "test" for all test functions (e.g.,
testSomething).
Assertion Use:
Assertions verify that actual outcomes match expectations.
Example of Assertion:
testOneEqualsOne(): Asserts that one is equal to one, showcasing trivial verification.For a
calculatorclass instance, testingadd(0, 1)should yield an outcome that equals one, confirming functional correctness.
Types of Assertions Found:
Assert that something is true.
Assert that something is false.
Assert equality of results.
Assert inequality of results.
Assertions generally follow similar formats across libraries.
Considerations for Effective Testing
Performance of Tests:
Tests should be swift and not involve database connections or remote website calls.
Aim to test logical processes rather than I/O operations like database access.
Regular practice and improvement in unit testing skills over time.
Workflow of Unit Testing
Before Code Submission:
Run all unit tests prior to integrating code into version control (e.g., GitHub).
Fix broken tests before pushing code changes to ensure stability in the shared codebase.
Conclusion
Unit testing is a crucial practice for ensuring code quality, and efficient testing can greatly enhance software development and maintenance.