agent: Write a simple python hello world Iteration 3
This commit is contained in:
@@ -2,6 +2,9 @@
|
|||||||
__pycache__/
|
__pycache__/
|
||||||
*.pyc
|
*.pyc
|
||||||
|
|
||||||
|
# Pytest
|
||||||
|
.pytest_cache/
|
||||||
|
|
||||||
# Distribution / packaging
|
# Distribution / packaging
|
||||||
dist/
|
dist/
|
||||||
build/
|
build/
|
||||||
|
|||||||
Binary file not shown.
@@ -1,30 +0,0 @@
|
|||||||
import subprocess
|
|
||||||
import unittest
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
|
||||||
class TestHello(unittest.TestCase):
|
|
||||||
def test_hello_output(self):
|
|
||||||
"""Test that hello.py prints 'Hello, World!' to stdout."""
|
|
||||||
result = subprocess.run(
|
|
||||||
["python3", "hello.py"],
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
cwd=Path(__file__).parent
|
|
||||||
)
|
|
||||||
self.assertEqual(result.returncode, 0)
|
|
||||||
self.assertIn("Hello, World!", result.stdout)
|
|
||||||
|
|
||||||
def test_hello_no_stderr(self):
|
|
||||||
"""Test that hello.py produces no stderr output."""
|
|
||||||
result = subprocess.run(
|
|
||||||
["python3", "hello.py"],
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
cwd=Path(__file__).parent
|
|
||||||
)
|
|
||||||
self.assertEqual(result.stderr, "")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
+33
-58
@@ -1,94 +1,69 @@
|
|||||||
"""Unit tests for hello.py module."""
|
"""Tests for hello.py module using pytest."""
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import unittest
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Module-level constants to avoid duplication across test functions
|
||||||
|
HELLO_SCRIPT = Path(__file__).parent.parent / "hello.py"
|
||||||
|
WORKING_DIR = Path(__file__).parent.parent
|
||||||
|
|
||||||
class TestHelloOutput(unittest.TestCase):
|
|
||||||
|
def run_hello():
|
||||||
|
"""Run hello.py and return the subprocess result."""
|
||||||
|
return subprocess.run(
|
||||||
|
["python3", str(HELLO_SCRIPT)],
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
cwd=WORKING_DIR,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestHelloOutput:
|
||||||
"""Tests for hello.py output behavior."""
|
"""Tests for hello.py output behavior."""
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up test fixtures."""
|
|
||||||
self.hello_script = Path(__file__).parent.parent / "hello.py"
|
|
||||||
self.working_dir = Path(__file__).parent.parent
|
|
||||||
|
|
||||||
def test_hello_prints_expected_message(self):
|
def test_hello_prints_expected_message(self):
|
||||||
"""Test that hello.py prints 'Hello, World!' to stdout."""
|
"""Test that hello.py prints 'Hello, World!' to stdout."""
|
||||||
result = subprocess.run(
|
result = run_hello()
|
||||||
["python3", str(self.hello_script)],
|
assert "Hello, World!" in result.stdout
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
cwd=self.working_dir
|
|
||||||
)
|
|
||||||
self.assertIn("Hello, World!", result.stdout)
|
|
||||||
|
|
||||||
def test_hello_exact_output(self):
|
def test_hello_exact_output(self):
|
||||||
"""Test that hello.py output matches exactly (with newline)."""
|
"""Test that hello.py output matches exactly (with newline)."""
|
||||||
result = subprocess.run(
|
result = run_hello()
|
||||||
["python3", str(self.hello_script)],
|
assert result.stdout.strip() == "Hello, World!"
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
cwd=self.working_dir
|
|
||||||
)
|
|
||||||
self.assertEqual(result.stdout.strip(), "Hello, World!")
|
|
||||||
|
|
||||||
def test_hello_exit_code_zero(self):
|
def test_hello_exit_code_zero(self):
|
||||||
"""Test that hello.py exits with code 0."""
|
"""Test that hello.py exits with code 0."""
|
||||||
result = subprocess.run(
|
result = run_hello()
|
||||||
["python3", str(self.hello_script)],
|
assert result.returncode == 0
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
cwd=self.working_dir
|
|
||||||
)
|
|
||||||
self.assertEqual(result.returncode, 0)
|
|
||||||
|
|
||||||
def test_hello_no_stderr(self):
|
def test_hello_no_stderr(self):
|
||||||
"""Test that hello.py produces no stderr output."""
|
"""Test that hello.py produces no stderr output."""
|
||||||
result = subprocess.run(
|
result = run_hello()
|
||||||
["python3", str(self.hello_script)],
|
assert result.stderr == ""
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
cwd=self.working_dir
|
|
||||||
)
|
|
||||||
self.assertEqual(result.stderr, "")
|
|
||||||
|
|
||||||
def test_hello_stdout_not_empty(self):
|
def test_hello_stdout_not_empty(self):
|
||||||
"""Test that hello.py produces non-empty stdout."""
|
"""Test that hello.py produces non-empty stdout."""
|
||||||
result = subprocess.run(
|
result = run_hello()
|
||||||
["python3", str(self.hello_script)],
|
assert len(result.stdout) > 0
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
cwd=self.working_dir
|
|
||||||
)
|
|
||||||
self.assertTrue(len(result.stdout) > 0)
|
|
||||||
|
|
||||||
|
|
||||||
class TestHelloFileExists(unittest.TestCase):
|
class TestHelloFile:
|
||||||
"""Tests for hello.py file existence and content."""
|
"""Tests for hello.py file existence and content."""
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
"""Set up test fixtures."""
|
|
||||||
self.hello_script = Path(__file__).parent.parent / "hello.py"
|
|
||||||
|
|
||||||
def test_hello_file_exists(self):
|
def test_hello_file_exists(self):
|
||||||
"""Test that hello.py file exists."""
|
"""Test that hello.py file exists."""
|
||||||
self.assertTrue(self.hello_script.exists())
|
assert HELLO_SCRIPT.exists()
|
||||||
|
|
||||||
def test_hello_file_is_python(self):
|
def test_hello_file_is_python(self):
|
||||||
"""Test that hello.py has .py extension."""
|
"""Test that hello.py has .py extension."""
|
||||||
self.assertEqual(self.hello_script.suffix, ".py")
|
assert HELLO_SCRIPT.suffix == ".py"
|
||||||
|
|
||||||
def test_hello_file_contains_print(self):
|
def test_hello_file_contains_print(self):
|
||||||
"""Test that hello.py contains a print statement."""
|
"""Test that hello.py contains a print statement."""
|
||||||
content = self.hello_script.read_text()
|
content = HELLO_SCRIPT.read_text()
|
||||||
self.assertIn("print", content)
|
assert "print" in content
|
||||||
|
|
||||||
def test_hello_file_contains_hello_world(self):
|
def test_hello_file_contains_hello_world(self):
|
||||||
"""Test that hello.py contains 'Hello, World!' string."""
|
"""Test that hello.py contains 'Hello, World!' string."""
|
||||||
content = self.hello_script.read_text()
|
content = HELLO_SCRIPT.read_text()
|
||||||
self.assertIn("Hello, World!", content)
|
assert "Hello, World!" in content
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user