Project that uses generative artificial intelligence models, such as ChatGPT-3.5, to streamline and improve the process of generating software test cases. It combines the ability of AI to generate ideas and preliminary drafts with review and validation by human experts.
It follows an agile and iterative methodology, where prompts are continuously refined to obtain high-quality test cases that cover all relevant scenarios. This allows optimizing resources, increasing testing efficiency without compromising quality, reducing costs and accelerating time-to-market.
Implementation
#Import openAI dependences (module)
import os
from openai import OpenAI
#Add our API Key copied from openAI page
client = OpenAI(
api_key='your_key',
)
def generate_test_cases(context, requirement, initial_test_cases):
# prompt (text/user's question)
prompt = f"Requirement: {requirement}\n\nInitial test cases: {initial_test_cases}\n\nImprove and expand the test cases. For each of the test cases identified in the previous stage, generate an initial draft that includes preconditions, steps to follow, and expected results"
response = client.chat.completions.create(
messages=[
{"role": "system", "content": context},
{"role": "user", "content": prompt}
],
model="gpt-3.5-turbo",
max_tokens=1024
)
improved_test_cases = response.choices[0].message.content.strip()
return improved_test_cases
def main():
# Make the context of our prompt environment (typo corrected: envoirement -> environment)
context = """
Software Test Engineer: Designs and executes comprehensive test plans for software.
"""
requirement = "Develop a login screen for users to access a web page. The screen should request a username and password, and validate that the entered data is correct. If the data is valid, the user should be redirected to the main page of the web application."
# Initial prompt to generate test cases
initial_prompt = f"Given the following requirement: {requirement}\n\nGenerate a list of possible test cases that should be considered to validate its correct operation."
# Get initial test cases
response = client.chat.completions.create(
messages=[
{"role": "system", "content": context},
{"role": "user", "content": initial_prompt}
],
model="gpt-3.5-turbo",
max_tokens=1024
)
initial_test_cases = response.choices[0].message.content.strip()
print("Initial test cases:\n", initial_test_cases)
# Iteration and refinement of test cases
for i in range(2):
improved_test_cases = generate_test_cases(context, requirement, initial_test_cases)
print(f"\nIteration {i + 1}:\n", improved_test_cases)
initial_test_cases = improved_test_cases
if __name__ == "__main__":
main()
Result
Initial Test Cases:
1. Positive Test Case: Enter a valid username and password and verify that it redirects to the main page.
2. Negative Test Case: Enter a valid username but an invalid password and verify that an error message is displayed.
3. Negative Test Case: Enter an invalid username and a valid password and verify that an error message is displayed.
4. Negative Test Case: Leave both the username and password fields blank and verify that error messages are displayed for both fields.
5. Negative Test Case: Enter special characters in the username field and verify that an error message is displayed.
6. Negative Test Case: Enter a short or too long password and verify that an error message is displayed.
7. Negative Test Case: Try to log in too many times with incorrect data and verify that the account is temporarily blocked.
8. Negative Test Case: Try to log in with a disabled user and verify that an appropriate message is displayed.
9. Negative Test Case: Try to log in without an internet connection and verify that an error message indicating the lack of connection is displayed.
10. Performance Test Case: Test the response speed when trying to log in with different levels of network traffic and verify that it is acceptable.
Iteration 1:
In addition to the initial test cases mentioned above, some additional test cases can be included:
11. Positive Test Case: Enter a username with mixed upper and lower case letters and a valid password, and verify that it redirects to the main page.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Enter a username with a combination of upper and lower case letters, a valid password, and click the login button.
- Expected result: The user is correctly redirected to the main page of the web application.
12. Negative Test Case: Try to log in with a blank username and a valid password, and verify that an error message is displayed.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Leave the username field blank, enter a valid password, and click the login button.
- Expected result: An error message is displayed indicating that the username field cannot be empty.
13. Negative Test Case: Try to log in with a valid username but with leading or trailing spaces and a valid password, and verify that it redirects to the main page.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Enter a username with leading or trailing spaces, a valid password, and click the login button.
- Expected result: The system removes the spaces and redirects the user to the main page.
14. Negative Test Case: Enter special characters in the password field and verify that an error message is displayed.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Enter a valid username, a password with special characters, and click the login button.
- Expected result: An error message is displayed indicating that the password cannot contain special characters.
15. Security Test Case: Try to perform a brute-force attack with multiple login attempts and verify that the account is temporarily blocked.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Enter a valid username but with incorrect passwords repeatedly.
- Expected result: After a certain number of failed attempts, the account should be temporarily blocked and display a message indicating the block.
These additional test cases cover specific functionality and security aspects that can be critical for the correct operation of the login screen in the web application.
Iteration 2:
16. Negative Test Case: Enter a valid username but an incorrect password and verify that an error message is displayed.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Enter a valid username, an incorrect password, and click the login button.
- Expected result: An error message is displayed indicating that the entered password is incorrect.
17. Negative Test Case: Try to log in with an invalid username and a valid password, and verify that an error message is displayed.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Enter an invalid username, a valid password, and click the login button.
- Expected result: An error message is displayed indicating that the entered username is invalid.
18. Negative Test Case: Leave both the username and password fields blank and verify that corresponding error messages are displayed for each field.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Leave both the username and password fields blank and click the login button.
- Expected result: Error messages are displayed indicating that both fields cannot be empty.
19. Usability Test Case: Check that when pressing the "Enter" key after entering the data, the form is submitted and the login attempt is made.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Enter a valid username, a valid password, and press the "Enter" key.
- Expected result: The form is submitted and the login attempt is made as usual.
20. Password Recovery Test Case: Verify that when clicking on a "Forgot your password?" link, a form to reset the password is presented.
- Precondition: The login page is loaded and ready to receive data.
- Steps to follow: Click on the "Forgot your password?" link and complete the form to reset the password.
- Expected result: A password recovery form is displayed and the process can be completed successfully.
These additional test cases cover more use scenarios and functionalities related to the login screen, which contributes to greater quality and robustness of it.
Run script with Jupyter Notebook or at https://colab.google/