Test Adlandırma Konvensiyaları
Yaxşı adlandırma konvensiyaları test oxunaqlığını artırır və developerlərə test məqsədlərini tez başa düşməyə kömək edir.
Test Adlandırma üçün Ümumi Prinsiplər
- Təsviri olun: Adlar nəyin test edildiyini aydın şəkildə göstərməlidir
- Tutarlı olun: Layihə boyu tutarlı naxış izləyin
- Qısa olun: Adlar məlumatverici olmalı, lakin həddindən artıq uzun olmamalıdır
- Davranışa diqqət yetirin: Adlar tətbiq təfərrüatlarını deyil, gözlənilən davranışı təsvir etməlidir
- Kontekst daxil edin: Adlar test edilən ssenari və ya şərtləri göstərməlidir
Test Class Naming Conventions
Common Patterns
| Pattern | Example |
|---|---|
[ClassUnderTest]Test | UserServiceTest |
Test[ClassUnderTest] | TestUserService |
[ClassUnderTest]Tests | UserServiceTests |
[ClassUnderTest]Spec | UserServiceSpec (common in BDD) |
[ClassUnderTest]Should | UserServiceShould (common in BDD) |
Language-Specific Conventions
Java (JUnit)
Koda bax
// Class under test: UserService
public class UserServiceTest { }
C# (.NET)
// Class under test: UserService
public class UserServiceTests { }
JavaScript/TypeScript (Jest)
// File naming: user-service.test.js or user-service.spec.js
describe('UserService', () => { });
Python (pytest)
# File naming: test_user_service.py
class TestUserService:
pass
Test Method Naming Conventions
Common Patterns
-
MethodName_StateUnderTest_ExpectedBehavior
@Test
void getUserById_UserExists_ReturnsUser() { } -
should_ExpectedBehavior_When_StateUnderTest
@Test
void should_ReturnUser_When_UserExists() { } -
test[Feature being tested]
@Test
void testUserRetrieval() { }
Java Examples
Koda bax
@Test
void getUserById_WithValidId_ReturnsCorrectUser() { }
@Test
void createUser_WithExistingUsername_ThrowsDuplicateException() { }
Naming in Different Testing Types
Unit Tests
Koda bax
@Test
void calculateTotal_WithValidItems_ReturnsSumOfPrices() { }
Integration Tests
Koda bax
@Test
void userController_SavesUserToDatabase_WhenValidUserSubmitted() { }
End-to-End Tests
Koda bax
@Test
void userRegistration_WithValidCredentials_CreatesAccountAndRedirectsToDashboard() { }
BDD-Style Naming (Given-When-Then)
Behavior-Driven Development uses a specific pattern:
Given [precondition]
When [action]
Then [expected result]
Example in Cucumber:
Scenario: User login with valid credentials
Given a user exists with username "john" and password "password123"
When the user attempts to login with username "john" and password "password123"
Then the user should be successfully logged in
And redirected to the dashboard page
Best Practices
- Avoid technical details in test names (e.g., avoid mentioning specific HTTP status codes)
- Avoid implementation details that might change (focus on behavior, not how it's implemented)
- Use domain language that business stakeholders can understand
- Be consistent within your team and project
- Update test names when requirements or behaviors change
- Group related tests using nested classes or describe blocks