LangChain provides a powerful way to generate both code and unit tests automatically using LLMs. This publication explores how LangChain generates a Python function for the Fibonacci sequence and its corresponding unit tests, showcasing AI-assisted software development.
When you run the script:
python main.py --language python --task "generate Fibonacci sequence"
--language
specifies the programming language (e.g., Python, JavaScript).--task
specifies the code to generate (e.g., "generate Fibonacci sequence").LangChain takes the arguments and constructs a prompt using a PromptTemplate
:
code_prompt = PromptTemplate( input_variables=["task", "language"], template="Write a standalone {language} function that will {task}. " "Only provide the function implementation with a proper function signature and return statement. " "Do not include any test cases, assertions, or example usages." )
Write a standalone Python function that will generate a Fibonacci sequence.
Only provide the function implementation with a proper function signature and return statement.
Do not include any test cases, assertions, or example usages.
def generate_fibonacci_sequence(n): if n <= 0: return [] elif n == 1: return [0] elif n == 2: return [0, 1] fibonacci_sequence = [0, 1] for i in range(2, n): next_num = fibonacci_sequence[i-1] + fibonacci_sequence[i-2] fibonacci_sequence.append(next_num) return fibonacci_sequence
Once the function is generated, it is passed into test_prompt
:
test_prompt = PromptTemplate( input_variables=["code", "language"], template="Write a full unit test in {language} using unittest for the following function. " "Include an import statement assuming the function is defined in a separate module named `your_module.py`. " "Only provide the test class and methods, do not include the function implementation itself:\n\n{code}" )
Write a full unit test in Python using unittest for the following function.
Include an import statement assuming the function is defined in a separate module named `fibonacci.py`.
Only provide the test class and methods, do not include the function implementation itself.
import unittest from fibonacci import generate_fibonacci_sequence class TestGenerateFibonacciSequence(unittest.TestCase): def test_generate_fibonacci_sequence_with_n_0(self): self.assertEqual(generate_fibonacci_sequence(0), []) def test_generate_fibonacci_sequence_with_n_1(self): self.assertEqual(generate_fibonacci_sequence(1), [0]) def test_generate_fibonacci_sequence_with_n_2(self): self.assertEqual(generate_fibonacci_sequence(2), [0, 1]) def test_generate_fibonacci_sequence_with_n_5(self): self.assertEqual(generate_fibonacci_sequence(5), [0, 1, 1, 2, 3]) def test_generate_fibonacci_sequence_with_n_10(self): self.assertEqual(generate_fibonacci_sequence(10), [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]) if __name__ == '__main__': unittest.main()
python fibonacci.py
✅ Method 1: Run Directly
python test_fibonacci.py
✅ Method 2: Using unittest
python -m unittest test_fibonacci.py
✅ Method 3: Run All Tests in the Directory
python -m unittest discover
LangChain successfully generated both a function and unit tests, reducing manual effort. However, human oversight is essential to refine AI-generated code, especially for edge cases.
✍️ Author: Mary Ann Dizon
🔗 Connect: LinkedIn Profile / GitHub
📌 Publication on Ready Tensor
There are no models linked
There are no datasets linked
There are no models linked
There are no datasets linked