Write a simple python hello world #5
+11
@@ -0,0 +1,11 @@
|
|||||||
|
# Python bytecode
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
# Pytest
|
||||||
|
.pytest_cache/
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
*.egg-info/
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
"""Tests package for hello.py."""
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
"""Tests for hello.py module using pytest."""
|
||||||
|
|
||||||
|
import subprocess
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
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."""
|
||||||
|
|
||||||
|
def test_hello_prints_expected_message(self):
|
||||||
|
"""Test that hello.py prints 'Hello, World!' to stdout."""
|
||||||
|
result = run_hello()
|
||||||
|
assert "Hello, World!" in result.stdout
|
||||||
|
|
||||||
|
def test_hello_exact_output(self):
|
||||||
|
"""Test that hello.py output matches exactly (with newline)."""
|
||||||
|
result = run_hello()
|
||||||
|
assert result.stdout.strip() == "Hello, World!"
|
||||||
|
|
||||||
|
def test_hello_exit_code_zero(self):
|
||||||
|
"""Test that hello.py exits with code 0."""
|
||||||
|
result = run_hello()
|
||||||
|
assert result.returncode == 0
|
||||||
|
|
||||||
|
def test_hello_no_stderr(self):
|
||||||
|
"""Test that hello.py produces no stderr output."""
|
||||||
|
result = run_hello()
|
||||||
|
assert result.stderr == ""
|
||||||
|
|
||||||
|
def test_hello_stdout_not_empty(self):
|
||||||
|
"""Test that hello.py produces non-empty stdout."""
|
||||||
|
result = run_hello()
|
||||||
|
assert len(result.stdout) > 0
|
||||||
|
|
||||||
|
|
||||||
|
class TestHelloFile:
|
||||||
|
"""Tests for hello.py file existence and content."""
|
||||||
|
|
||||||
|
def test_hello_file_exists(self):
|
||||||
|
"""Test that hello.py file exists."""
|
||||||
|
assert HELLO_SCRIPT.exists()
|
||||||
|
|
||||||
|
def test_hello_file_is_python(self):
|
||||||
|
"""Test that hello.py has .py extension."""
|
||||||
|
assert HELLO_SCRIPT.suffix == ".py"
|
||||||
|
|
||||||
|
def test_hello_file_contains_print(self):
|
||||||
|
"""Test that hello.py contains a print statement."""
|
||||||
|
content = HELLO_SCRIPT.read_text()
|
||||||
|
assert "print" in content
|
||||||
|
|
||||||
|
def test_hello_file_contains_hello_world(self):
|
||||||
|
"""Test that hello.py contains 'Hello, World!' string."""
|
||||||
|
content = HELLO_SCRIPT.read_text()
|
||||||
|
assert "Hello, World!" in content
|
||||||
Reference in New Issue
Block a user