Unit Testing for Developers: Write Better Code, Catch Bugs Early

Unit Testing for Developers: Write Better Code, Catch Bugs Early

Unit Testing කියන්නේ software development process එකේ අතිශය වැදගත් concept එකක්. ඔයා developer කෙනෙක් නම් මේ ගැන අනිවාරෙන්ම දැනගන්නෝනි. අපි මේ article එකෙන් කතා කරන්න යන්නේ unit testing ගැන. ඉස්සෙල්ලාම බලමු මොකද්ද මේ unit testing කියන්නෙ කියලා. unit testing කියන්නේ program එකේ එක එක components හෝ functions ස්වාභාවික ලෙස නිවැරදිව function වෙනවද කියලා තනි තනිව (individually) check කරන testing process එකයි. Unit testing එකේ අරමුණ තමා code quality එක වැඩි කරන එක හා bugs development process එකේ පළමු අදියරේදී හඳුනා ගැනීමට උදව් කරන එක. මේ වගේ testing එකක් නිසා future changes (refactoring) කිරීමේදිත්, software maintain කිරීමේදීත් risk එක අඩු වෙනවා.

Unit Testing vs Other Types of Testing:

  • Integration Testing: Integration Testing කියන්නේ individually හදපු modules හෝ components එකට එකතු කරලා එක group එකක් ලෙස test කිරීමක්. මෙහි අරමුණ modules අතර data flow එක සහ communication එක නිවැරදිව function වෙනවාද කියලා verify කර ගැනීම.
  • System Testing: System Testing කියන්නේ සම්පූර්ණ software පද්ධතිය එකම unit එකක් ලෙස test කිරීමක්. මේකෙදි system එක user requirements වලට අනුකූලද හා විවිධ condition වලදී නිවැරදි ලෙස වැඩ කරනවද කියලා test කරනවා.
  • End-to-End Testing: End-to-End Testing කියන්නේ user කෙනෙකුගේ perspective එකෙන් system එක සම්පූර්ණයෙන් test කිරීමක්. මෙහිදී front-end සිට back-end දක්වා complete process එක user actions සහ scenarios අනුව simulation කරලා test කරනවා.

මේ context එකේ, unit testing කියන්නේ smallest unit of code එකක් focus කරලා test කරන testing type එක කියලා හඳුන්වන්න පුලුවන්.

එතකොට මොනවද මේ Unit Testing කරන එකෙන් ලැබෙන benifits

  1. Improves Code Quality and Reliability - Unit tests එකක් තිබීම නිසා, අපේ code එක predictable ලෙස වැඩ කරයි. එකම function එක test කරන නිසා, unexpected errors හෝ behavior මතු වීම අඩු වෙනවා.
  2. Makes Refactoring Safer - Code structure එක වෙනස් කළත් (refactoring) unit tests run කිරීමෙන්, අපේ changes නිවැරදිද කියලා confirm කර ගන්න පුලුවන්. ඒ නිසා refactoring process එක much safer වෙනවා.
  3. Detects Bugs Early in Development - Bug එක software release වීමට පෙර detect කිරීමෙන්, future maintenance cost අඩු කරනවා. Early detection එක code quality එක වැඩි කරනවා.
  4. Acts as a Form of Documentation - Tests කියන්නේ function හෝ module එකක් expected behavior එක දැක්වීමේ විධියක්. නිවැරදි test cases තිබීමෙන්, අලුත් developer කෙනෙක්ට function එක කොහොමද වැඩ කරන්නේ කියලා ලේසියෙන් තේරුම් ගන්න පුළුවන්.
  5. Reduces the Cost of Fixing Bugs - Bug එක development stage එකේ detect කරන එක production stage එකේ detect කරන එකට වඩා significantly cheaper. ඒකෙන් Unit Testing එක future maintenance costs අඩු කරනවා.

Key Concepts

Unit Testing කිරීමට පෙර දැන ගත යුතු Key Concepts කිහිපයක් තියෙනවා:

  1. Test Case - Test Case කියන්නේ program එකේ special function එකක් හෝ feature එකක් හරියට වැඩ කරනවාද කියලා check කරන condition එකක්. උදාහරණයක් විදියට add(a, b) function එකට 2 සහ 3 දීලා result එක 5ද කියලා බලන එක Test Case එකක්.
  2. Test Suite - Test Suite කියන්නේ එකම category එකට අයත් multiple Test Cases එකට එකතු කරලා තියෙන එකක්. උදාහරණයක් විදියට MathFunctions Test Suite එකට add(), subtract(), multiply() වගේ functions ට Test Cases එකතු කරලා තියෙන්න පුළුවන්.
  3. Mocking සහ Stubbing - මේක external dependencies (උදාහරණයක් විදියට database, API calls වගේ) simulate කරන්න භාවිතා කරන ක්රමය. මෙයින් test එක run වෙන කොට real system එකට connect නොවී isolated environment එකකම run වෙන නිසා test එක safe සහ fast වෙනවා. උදාහරණයක් විදියට real database එකකට connect වෙන්නෙ නැතුව fake/mock database එකක් use කරලා test එක run කරනවා.
  4. Assertions - Assertions කියන්නේ test එකේ expected result එක actual result එකට සමානද කියලා check කරන statement එකක්. උදාහරණයක් විදියට expect(add(2,3)).toBe(5) කියන්නේ result එක 5 ද යන්න verify කරන Assertion එකක්.
  5. Code Coverage - Code Coverage කියන්නේ test cases වලින් program එකේ කොපමණ code portion එකක් test කරලා තියෙනවාද කියලා measure කරන විදිහක්. Coverage percentage එක වැඩි වෙනකොට, වැඩි code portion එකක් test කරලා තියෙන නිසා system එක reliability බව වැඩි වෙනවා.

Unit Testing Best Practices

Unit Testing වලින් maximum benefit එකක් ලබා ගන්න නම්, developer කෙනෙක් follow කරන්න ඕනි best practices කිහිපයක් තියෙනවා.

  1. Keep Tests Small and Focused - එක් test එකක් එකම functionality එකටම focus වෙන්න ඕනේ. අනික් දෙයක් එකම test එකේ mix කරන්න එපා.  උදාහරණයක් විදියට, add(a, b) function එකේ positive numbers test එකක් වෙනම, negative numbers test එකක් වෙනම ලියන්න.
  2. Tests එකිනෙකට independent වෙන්න ඕනි - Test එකක්, අනික් test එකක result මත depend නොවිය යුතුයි. මේකෙන් test run order වෙනස් වුණත්, result එක consistent වෙනවා.
  3. Descriptive names භාවිතා කරන්න - Test case එකේ නම function එකේ behavior එක විස්තර කරන්න ඕනේ. Simple names වලින් code එක confuse වෙන්න පුළුවන්. උදාහරණයක් විදියට testAddition_withPositiveNumbers_returnsCorrectSum කියලා descriptive name එකක් use කරන්න.
  4. Write Tests Before (TDD) or After Implementation - Test-Driven Development (TDD) approach එකෙන් tests ලියලා පසුව code implement කරන්න පුළුවන්. ඒ වගේම Implementation එකෙන් පසුව tests ලියලා existing code එක verify කරන්න පුළුවන්. දෙකම acceptable, නමුත් TDD approach එකෙන් code quality වැඩි වෙනවා.
  5. Implementation details test නොකරන්න, behavior එක focus කරන්න - Test එකකින් code එක කොහොම වැඩ කරනවා බලනවාට වඩා, කොහොම result එකක් return කරනවාද කියලා check කරන්න. උදාහරණයක් විදියට calculateTotal() function එකේ internal loop details test නොකර, return කරන total value එක verify කරන්න.

Popular Unit Testing Frameworks

දැන් බලමු අපි Popular Unit Testing Frameworks මොනවද කියලා. Unit Testing කරන්න විවිධ programming languages සඳහා frameworks තියෙනවා. ඒවා developers ලා testing process එක පහසු කරනවා වගේම වැඩි reliable results ලබාගන්නත් උපකාරී වෙනවා.

  • JavaScript/Node.js: Jest, Mocha, Jasmine – Node.js සහ frontend projects සඳහා use කරනවා.
  • Python: unittest, pytest – Python applications සඳහා popular frameworks.
  • Java: JUnit, TestNG – Java applications testing සඳහා widely used frameworks.
  • C#/.NET: NUnit, MSTest, xUnit – .NET applications testing සඳහා strong support සහ integration tools ඇත.

මෙම frameworks, unit tests ලියන process එක simplify කරනවා, ඒ වගේම test execution automate කරනවා සහ results reporting පහසු කරනවා.

Common Mistakes to Avoid

Unit Testing කරන විට developers අතින් සිද්ද වෙන Common Mistakes කිහිපයක් තියෙනවා.

  1. Complex tests ලිවීම(hard to maintain) - Test එක too complex වුණොත්, future changes සමඟ maintain කරන්න අමාරු වෙනවා. ඒකට simple, focused, සහ readable test ලියන එකම හොඳම විදිය.
  2. External dependencies isolate නොකිරීම - Database, API calls වගේ external resources without mocking test කරනවා නම් tests inconsistent වෙන්න පුලුවන්. ඒ නිසා mocking සහ stubbing techniques use කරන්න. මේකෙන් tests safe, fast, සහ predictable වෙනවා.
  3. Edge cases අවම වශයෙන් consider කිරීම - Function behavior unusual or extreme inputs වලට test නොකළහොත්, production issues ඇති විය හැක. Example: negative numbers, empty inputs, null values. Function එක unusual inputs වලට හෝ extreme values වලට test නොකරොත්, production stage එකේදි issues ඇති වෙන්න පුලුවන්. උදාහරණයක් විදියට negative numbers, empty inputs, null values check කරන එක.
  4. Manual testing වල පමණක් rely වීම - Automated unit tests නොතිබ්බොත් bugs detect කිරීම slow වෙනවා සහ human error risk වැඩි වෙනවා.

අවසානයේ කියන්න පුළුවන් Unit Testing කියන්නේ software development process එකේ වැදගත්ම කොටසක්. එය developer කෙනෙකුට තම code එකේ Reliability බව වැඩි කරගන්න, early-stage bugs  detect කරගන්න, හා long-term maintenance easy කරගන්න උපකාරී වෙනවා.

මේ Github Repo Link එකට ගියොත් Node.js backend හා React.js frontend එකකට unit test cases jest use කරලා implement කරන විදිය ගැන දැන ගන්නත් පුලුවන්.

Github Link

To view or add a comment, sign in

More articles by Chamaththa Shamod

Others also viewed

Explore content categories