“Tests are something you know you should be doing, but haven’t had the time to learn, or tried to and didn’t get very far.”
- To know what exactly is unit testing, to know about its applications and to write some tests live(if time allows)
- Introduction to what’s linting and how you can extend it to your favourite text editor
- Auto Formatters and naming conventions
- All examples shown using Python
- unittest (python inbuilt)
- math (python inbuilt)
- flake8 (linter)
- yapf (auto formatter)
areas ├── areas │ ├── area.py │ └── __init__.py └── tests └── __init__.py
mkdir -p areas/{areas,tests}
cd areas
touch areas/__init__.py areas/area.py tests/__init__.py
from math import pi
def circle_area(r):
return pi * (r**r)
radii = [0,1,0.1,2+3j,-7,True,'a']
for radius in radii:
print(circle_area(radius))
No. Since radii here are simply the value of distance from center of circle to its circumference. Thus it should be +ve.
import unittest
from math import pi
from areas import area
class TestCircleArea(unittest.TestCase):
def test_radius(self):
result = area.circle_area(1)
self.assertEqual(result,pi)
If the test outputs OK then our function definition is correct
We can raise ValueError on intercepting a negative value as radius
"""
in test file
"""
def on_negative_radii(self):
self.assertRaises(ValueError,area.circle_area,-7)
"""
in area file, inside circle_area function, add the following
"""
if r<0:
raise ValueError
We can raise TypeError on intercepting a invalid datatype value
"""
in test file
"""
def on_other_types(self):
self.assertRaises(TypeError,area.circle_area,"Hello")
"""
in area file, inside circle_area function, add the following
"""
if type(r) not in [int,float]:
raise TypeError
Create an unittest, which can be used with all programs done using Python Sockets, in Computer Networks Lab
Example : Most people manually set the IP Address of the system while creating a socket. So when changing the system, the ip will also change, and will have to be manually changed again. Write a test case to make sure the IP Address is always same as that of the system.
Language | Linter |
---|---|
C | Clangd |
C++ | Clangd/CppCheck |
Python | Flake8/Pylint |
Java | CheckStyle |
Shell Script | Shellcheck |
It’s the process of automatically formatting the code to follow a particular style guide and coding convention
Language | Formatter |
---|---|
C | Clang-Format |
C++ | Clang-Format |
Python | Yapf/Black |
Java | Google-Java-Format |