Python
Version
Running on Python v3.11.8
Supported languages
Python
Testing framework
Special reminders and implementation details
from pytest import mark as m
from challenge.app import helloWorld
@m.describe("app.py")
class TestHelloWorld:
@m.it("Should return 'Hello World!'")
def test_return_hello_world(self):
assert helloWorld() == 'Hello World!'Included libraries
Write-tests challenges
This template supports write-tests challenges. The reference solution is already written and you write the tests: they must pass on the reference solution and catch every hidden bug. Run Tests runs your tests against the reference solution only, while Verify tests and Submit also run them against hidden copies of it that each contain one bug. Read Solving a write-tests challenge for the full flow.
- Test runner: pytest
- Import the solution through the
challengepackage, for examplefrom challenge.clamp import clamp. The Run entry (main.py) imports without the prefix (from clamp import clamp). - Name test files
test_*.py. The@m.describe/@m.itdecorators frompytest.markgive your tests readable names in the results panel. - Use
pytest.raisesto pin down error behavior: a missing validation is a classic hidden bug.
A complete example is in Write-tests examples by template.
Reading input (stdin)
Programs that read values use standard input. Run opens an interactive terminal: the program starts, prints its prompt, and you type the answer and press Enter, as in a real terminal. The tests of an output-test challenge feed the input themselves.
name = input("Your name: ")
age = int(input("Your age: "))
print(f"Hello, {name}! Next year you will be {age + 1}.")input() returns the line without its newline and raises EOFError at end-of-file. In output tests the prompt text passed to input() is hidden from the compared output, so Python programs may prompt.
Browser preview: when the challenge or project has the web preview on, input() in the Preview tab opens the browser's own dialog with the prompt text; the answer is echoed in the preview terminal, and Cancel ends the input with EOFError. sys.stdin.readline() opens the same dialog.
How to debug
Debugging Python code is essential for every developer. One of the most straightforward yet effective ways to debug is by using print() statements.
Steps to Debug Using Print Statements
Identify the Problem Area: Locate the section of your code where you suspect the problem might be.
Insert
print()Statements: Addprint()statements before and after the suspected problematic code to print out variable values and flow control.Check the Results Output of your tests
Analyze the Output: Look at the values printed in the results console to understand what's going wrong in your code. Adjust your code based on the findings and repeat as necessary.
Here’s an example of how to use print() for debugging:
def calculate_sum(a, b):
print("calculate_sum called with arguments:", a, b) # Log function call
sum_val = a + b
print("Sum after addition:", sum_val) # Log sum calculation
return sum_val
result = calculate_sum(5, 10)
print("Result of calculate_sum:", result) # Log the result