Adding Tests to the genai-stack Codebase
This document provides guidance on how to add tests to the genai-stack codebase.
Testing Python Code
For Python code, consider using the unittest
framework built into Python.
Example:
import unittest
def add(x, y):
"""Adds two numbers together."""
return x + y
class TestAdd(unittest.TestCase):
def test_add_positive(self):
self.assertEqual(add(2, 3), 5)
def test_add_negative(self):
self.assertEqual(add(-2, -3), -5)
if __name__ == '__main__':
unittest.main()
Running Tests:
To run the tests, execute the following command:
python -m unittest test_add.py
This will run all the test methods defined in the TestAdd
class.
Testing Svelte Components
Svelte doesn’t have a built-in testing framework, but you can leverage libraries like @testing-library/svelte
and jest
for testing your Svelte components.
Example:
// src/components/MyComponent.svelte
<script>
export let message = 'Hello world!';
</script>
<p>{message}</p>
// src/components/MyComponent.test.js
import { render, screen } from '@testing-library/svelte';
import MyComponent from './MyComponent.svelte';
describe('MyComponent', () => {
test('renders the message', () => {
render(MyComponent, { message: 'Hello, Test!' });
expect(screen.getByText('Hello, Test!')).toBeInTheDocument();
});
});
Running Tests:
To run the tests, execute the following command:
npm run test:unit
Testing Shell Scripts
For shell scripts, you can use a framework like bats
for testing.
Example:
#!/usr/bin/env bats
@test "addition" {
run bash script.sh 2 3
assert_success
assert_output "5"
}
Running Tests:
To run the tests, execute the following command:
bats test.bats
Testing JavaScript Code
For JavaScript code, consider using the jest
framework.
Example:
// src/scripts/utils.js
function add(x, y) {
return x + y;
}
// src/scripts/utils.test.js
import { add } from './utils';
describe('add', () => {
it('adds two numbers', () => {
expect(add(2, 3)).toBe(5);
});
});
Running Tests:
To run the tests, execute the following command:
npm run test:unit
Testing Dockerfile
While Dockerfiles are not directly testable, you can use testing tools like docker-compose
to test the functionality of the application built by the Dockerfile.
Example:
version: "3.7"
services:
app:
build: .
ports:
- "8080:80"
environment:
- APP_ENV=test
depends_on:
- database
database:
image: postgres:latest
You can then use docker-compose up
to start the application and test it against the specified configurations.
Testing HTML and CSS
For HTML and CSS, consider using visual regression testing tools like Percy
or BackstopJS
. These tools allow you to capture screenshots of your web pages and compare them to previous versions to detect any visual regressions.
Example:
// cypress/integration/homepage.spec.js
describe('Homepage', () => {
it('renders correctly', () => {
cy.visit('/');
cy.percySnapshot('Homepage');
});
});
Running Tests:
To run the tests, execute the following command:
npx cypress run
This will run the Cypress tests and capture screenshots for visual comparison.
Remember to adapt these examples and implement your own test cases to cover the specific functionality and requirements of your project.