Unit Testing
Unit Testing
Introduction
Unit tests are automated and are run each time the code is changed to ensure that
new code does not break existing functionality. Unit tests are designed to validate
the smallest possible unit of code, such as a function or a method, and test it in
isolation from the rest of the system. This allows developers to quickly identify and
fix any issues early in the development process, improving the overall quality of the
software and reducing the time required for later testing.
Objective of Unit Testing
The objective of Unit Testing are follows
o To isolate a section of code.
o To verify the correctness of the code.
o To test every function and procedure.
o To fix bugs early in the development cycle and to save costs.
o To help the developers understand the code base and enable them to
make changes quickly.
o To help with code reuse.
1. Manual Testing
2. Automation Testing
To apply unit testing in your Django REST Framework (DRF) API,
you can follow these steps:
class BarberShopAPITest(TestCase):
def setUp(self):
self.client = APIClient()
def test_create_barbershop(self):
"""
Test for creating a barber shop via API
"""
url = reverse('barberRegisterPage') # Ensure this matches the URL
name in your urls.py
# Create a sample image file for logo upload
logo_path = 'E:\\Broader ai\\Carely-
backend\\servercarely\\media\\shopLogo\\2.jpg'
with open(logo_path, 'rb') as logo_file:
logo = SimpleUploadedFile(
name=os.path.basename(logo_path),
content=logo_file.read(),
content_type='image/jpeg'
)
random_uuid = uuid.uuid4()
data = {
"carely_barber_shop_id": random_uuid,
"carely_barber_shop_commercial_number": "34566",
"carely_barber_shop_name": "Vogue cut",
"carely_barber_shop_uploadLogo": logo,
"carely_barber_shop_type": "individual",
"carely_barber_shop_admin_email": "[email protected]",
"carely_barber_shop_admin_password": "John@1234"
}
def test_barbershop_email_validation(self):
"""
Test for invalid barber shop email
"""
url = reverse('barberRegisterPage')
random_uuid = uuid.uuid4()
data = {
"carely_barber_shop_id": random_uuid,
"carely_barber_shop_commercial_number": "34566",
"carely_barber_shop_name": "Vogue cut",
"carely_barber_shop_type": "individual",
"carely_barber_shop_admin_email": "invalid-email", # Invalid
email
"carely_barber_shop_admin_password": "John@1234"
}
def test_duplicate_commercial_number(self):
"""
Test for duplicate barber shop commercial number
"""
# Create a barber shop first
random_uuid = uuid.uuid4()
BarberShopModel.objects.create(
carely_barber_shop_id = random_uuid,
carely_barber_shop_commercial_number="34566",
carely_barber_shop_name="Vogue cut",
carely_barber_shop_type="individual",
carely_barber_shop_admin_email="[email protected]",
carely_barber_shop_admin_password="hashed_password"
)
url = reverse('barberRegisterPage')
data = {
"carely_barber_shop_id": random_uuid,
"carely_barber_shop_commercial_number": "34566", # Duplicate
commercial number
"carely_barber_shop_name": "New Cut",
"carely_barber_shop_type": "individual",
"carely_barber_shop_admin_email": "[email protected]",
"carely_barber_shop_admin_password": "NewPass@123"
}
def test_invalid_password(self):
"""
Test for weak password
"""
url = reverse('barberRegisterPage')
random_uuid = uuid.uuid4()
data = {
"carely_barber_shop_id": random_uuid,
"carely_barber_shop_commercial_number": "34566",
"carely_barber_shop_name": "Vogue cut",
"carely_barber_shop_type": "individual",
"carely_barber_shop_admin_email": "[email protected]",
"carely_barber_shop_admin_password": "weakpass" # Weak password
}
APIURL=" http://127.0.0.1:8000"
Reviewuserintoregister = f"{APIURL}/barberApis/BarberShopGet"
response = requests.post(Reviewuserintoregister)
print(response)