Skip to main content

Unit Test Generation

  1. Basic Unit Test Generation

Template:

/test in [language] using [testing framework] for the following function or method. Ensure that all standard cases and edge cases are covered:

[code snippet]

Example:

/test in Python using pytest for the following function that calculates the factorial of a number. Ensure that edge cases (e.g., negative numbers, zero) are covered:

def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
  1. Testing Asynchronous Functions

Template:

/test in [language] using [testing framework] for the following asynchronous function. Ensure that asynchronous behavior and potential race conditions are tested:

[code snippet]

Example:

/test in JavaScript using Jest for the following asynchronous function that fetches user data from an API. Ensure that both successful responses and error scenarios are tested:

async function fetchUserData(userId) {
const response = await fetch(`https://api.example.com/users/${userId}`);
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
}
  1. Testing React Components

Template:

/test for the following React component using [testing library] in [language]. Ensure that all interactive elements and state changes are properly tested:

[code snippet]

Example:

/test for the following React component using React Testing Library in JavaScript. Ensure that the button click updates the counter correctly:

import React, { useState } from 'react';

function Counter() {
const [count, setCount] = useState(0);

return (
<div>
<p data-testid="count">Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}

export default Counter;
  1. Mocking Database Interactions

Template:

/test in [language] using [testing framework] for the following function that interacts with a [database type] database. Use [mocking library/tool] to mock the database interactions:

[code snippet]

Example:

/test in Python using unittest for the following function that retrieves a user from a PostgreSQL database. Use unittest.mock to mock the database interactions:

import psycopg2

def get_user(user_id):
conn = psycopg2.connect(database="testdb", user="user", password="pass")
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
user = cursor.fetchone()
conn.close()
return user
  1. Testing with External Services Integration

Template:

/test in [language] using [testing framework] for the following function that integrates with [external service/tool]. Use [mocking library/tool] to simulate the external service responses:

[code snippet]

Example:

/test in JavaScript using Jest for the following function that logs errors to Sentry. Use jest.mock to simulate Sentry's error logging:

const CoralogixLogger = require('coralogix-logger');

function processData(data) {
try {
// Process data
if (!data) throw new Error('No data provided');
// More processing...
} catch (error) {
CoralogixLogger.error(error.message);
throw error;
}
}
  1. Testing Database Queries

Template:

/test in [language] using [testing framework] for the following function that performs [database operation] on [database type]. Ensure that queries are executed correctly and handle possible exceptions:

[code snippet]

Example:

/test in C# using xUnit for the following function that inserts a new record into a MySQL database. Ensure that the insertion is performed correctly and handle potential SQL exceptions:

using MySql.Data.MySqlClient;

public void InsertUser(string name, string email) {
string connStr = "server=localhost;user=root;database=test;port=3306;password=PASSWORD";
using (var conn = new MySqlConnection(connStr)) {
conn.Open();
string sql = "INSERT INTO users (name, email) VALUES (@name, @email)";
using (var cmd = new MySqlCommand(sql, conn)) {
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@email", email);
cmd.ExecuteNonQuery();
}
}
}
  1. Testing with Caching Mechanisms

Template:

/test in [language] using [testing framework] for the following function that utilizes [caching mechanism/tool]. Ensure that caching behavior (e.g., cache hits and misses) is correctly tested:

[code snippet]

Example:

/test in Python using pytest for the following function that uses Redis as a caching mechanism. Ensure that both cache hits and cache misses are properly handled:

import redis

r = redis.Redis(host='localhost', port=6379, db=0)

def get_user_data(user_id):
cached_data = r.get(f"user:{user_id}")
if cached_data:
return cached_data
# Simulate database call
user_data = {"id": user_id, "name": "John Doe"}
r.set(f"user:{user_id}", user_data)
return user_data
  1. Testing Error Handling and Logging

Template:

/test in [language] using [testing framework] for the following function that includes error handling and logging with [logging tool/service]. Ensure that errors are correctly caught and logged:

[code snippet]

Example:

/test in JavaScript using Mocha and Sinon for the following function that handles errors and logs them using Coralogix. Ensure that errors are properly caught and logged:

const CoralogixLogger = require('coralogix-logger');

function processData(data) {
try {
// Process data
if (!data) throw new Error('No data provided');
// More processing...
} catch (error) {
CoralogixLogger.error(error.message);
throw error;
}
}
  1. Testing with Continuous Integration (CI) Pipelines

Template:

/test in [language] using [testing framework] for the following project. Additionally, provide a basic [CI tool] configuration file to run these tests automatically on each commit:

[code snippet]

Example:

/test in Python using pytest for the following function and provide a basic Jenkinsfile to run these tests automatically on each commit:

def add(a, b):
return a + b

Jenkinsfile:

pipeline {
agent any
stages {
stage('Install Dependencies') {
steps {
sh 'pip install -r requirements.txt'
}
}
stage('Run Tests') {
steps {
sh 'pytest'
}
}
}
}
  1. Testing Monitoring and Metrics Integration

Template:

/test in [language] using [testing framework] for the following function that integrates with [monitoring tool/service]. Ensure that metrics are correctly recorded and handled:

[code snippet]

Example:

/test in Go using the testing package for the following function that records metrics using Prometheus. Ensure that metrics are correctly incremented:

import (
"github.com/prometheus/client_golang/prometheus"
)

var (
requestCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Number of HTTP requests",
},
)
)

func init() {
prometheus.MustRegister(requestCounter)
}

func handleRequest() {
requestCounter.Inc()
// Handle the request...
}