Asked by K

Function analysis unit test

All Answers 1

Answered by GPT-5 mini AI
I’m not sure exactly what you mean by “Function analysis unit test” — do you want a unit test for a specific function, a template for writing such tests, or guidance on how to design tests for a function-analysis routine? Tell me the language, test framework, and the function signature/behavior and I’ll produce concrete tests.

Below are: (A) a short checklist / guidelines for writing good unit tests for a function that “analyzes” input, and (B) ready-to-run example unit tests in Python (pytest) and JavaScript (Jest). If you want a different language/framework or a test for a particular function you already have, paste the function and I’ll write tests specifically for it.

A — Checklist & test types to include
- Happy-path: typical inputs that exercise normal behavior. Assert exact expected outputs.
- Edge cases: empty inputs, single-element inputs, minimum/maximum values, very long inputs.
- Error handling: inputs that should raise exceptions or return error codes.
- Boundary values: off-by-one and limits.
- Invalid types: e.g., None/null, wrong types — confirm appropriate error/handling.
- Determinism & idempotence: repeated calls produce same output for same input.
- Performance/sanity: very large inputs (smoke test), timeout if needed.
- Properties: where exact output varies, test invariants (sum of parts equals total, order-preserving, etc.)
- Isolation: mock external dependencies (network, DB, time) so unit tests are deterministic.

B — Example test suites

1) Python (pytest)
Assume function analyze_text(text: str) -> dict with keys: word_count (int), sentence_count (int), most_common_word (str or None).

tests/test_analyze_text.py
```
import pytest
from mymodule import analyze_text

def test_happy_path():
text = "Hello world. Hello!"
out = analyze_text(text)
assert out["word_count"] == 3
assert out["sentence_count"] == 2
assert out["most_common_word"] == "hello" # assume function lowercases

def test_empty_string():
out = analyze_text("")
assert out["word_count"] == 0
assert out["sentence_count"] == 0
assert out["most_common_word"] is None

def test_punctuation_and_unicode():
text = "Café — café. ¿Qué tal?"
out = analyze_text(text)
assert out["word_count"] == 4
assert out["most_common_word"] == "café"

def test_none_raises():
with pytest.raises(TypeError):
analyze_text(None)

def test_large_input_performance():
large = "word " * 100_000
out = analyze_text(large)
assert out["word_count"] == 100_000
```

Notes:
- Use parametrized tests for many input/output pairs.
- If analyze_text depends on other modules (e.g., external tokenizer), patch them with pytest-mock or unittest.mock.

2) JavaScript (Jest)
Assume function analyzeNumbers(arr) returns { min, max, avg }.

tests/analyzeNumbers.test.js
```javascript
const analyzeNumbers = require('../analyzeNumbers');

test('happy path', () => {
const res = analyzeNumbers([1, 2, 3, 4]);
expect(res.min).toBe(1);
expect(res.max).toBe(4);
expect(res.avg).toBeCloseTo(2.5);
});

test('single element', () => {
const res = analyzeNumbers([5]);
expect(res.min).toBe(5);
expect(res.max).toBe(5);
expect(res.avg).toBe(5);
});

test('empty array throws', () => {
expect(() => analyzeNumbers([])).toThrow('empty input');
});

test('invalid input types', () => {
expect(() => analyzeNumbers(null)).toThrow();
expect(() => analyzeNumbers([1, 'a'])).toThrow();
});
```

3) Notes on mocking external deps
If your function performs I/O or calls a service, keep unit tests isolated:
- Python: use unittest.mock.patch or pytest-mock to replace network/DB calls with fakes.
- JS: use jest.mock(...) and provide fake implementations.

If you tell me:
- the language/framework,
- the exact function signature and expected behavior (including edge-case behavior and error conditions),
I’ll write a focused unit test file (or multiple tests) tailored to your function.