寻求可自动校验程序输出与预期输出的工具(附相关截图)
Hey there! I totally get needing a reliable way to check if your code's output matches what's expected—especially for online courses where you can't rely on your university's tool everywhere. Here are some great options that fit the bill, categorized by how you might want to use them:
Local IDE & Editor Solutions
These let you work directly in your preferred environment:
- VS Code: Use extensions like Code Runner paired with custom test scripts, or language-specific extensions (like Python Test Explorer for Python). You can set up test cases in a separate file, run your program, and compare outputs using simple helper functions or built-in commands.
- PyCharm/IntelliJ IDEA: Both have robust built-in testing frameworks. For Python, you can use
unittestorpytestto define test cases that run your code and assert against expected outputs. For Java, JUnit integration is seamless—just write test classes that invoke your main method and check results. - Eclipse: Similar to IntelliJ, it supports JUnit for Java and has plugins for other languages to set up automated tests.
Command-Line Tools
Perfect if you prefer working in a terminal:
- Bash/Zsh Scripts: Write a simple script that runs your program with input, captures the output, and uses the
diffcommand to compare it against your expected output file. Example:
If there's no output from./your_program < input.txt > output.txt diff output.txt expected.txtdiff, your results match! - pytest (Python): Create test functions that call your code with specific inputs and use
assertstatements to check outputs. For example:
Run it withdef test_my_function(): assert my_function(5) == "expected_result"pytest test_file.pyand it'll report any mismatches. - JUnit (Java): Write test classes that execute your code and verify results using
assertEqualsmethods.
Web-Based Tools
Great for when you don't want to set up local environments:
- Replit: You can create a project, write your code, and add a separate test script that runs your program with test inputs and compares outputs. It supports almost all popular languages.
- OnlineGDB: Besides running code, you can set up custom input and compare the output manually, or write a small test harness within the same code window to automate checks.
- Codewars/HackerRank: While these are more for challenges, many let you write your own test cases (in addition to the provided ones) to validate your code before submitting.
Custom Test Harness
If you want full control, write a simple test harness in your programming language. For example, in Python:
def run_test(input_data, expected_output): # Run your program with input_data and capture output output = your_program_function(input_data) if output == expected_output: print("Test Passed!") else: print(f"Test Failed. Expected: {expected_output}, Got: {output}") # Define test cases test_cases = [ ("input1", "expected1"), ("input2", "expected2") ] for input_val, expected in test_cases: run_test(input_val, expected)
Pick the option that fits your workflow best—all of these will help you quickly verify if your code is producing the right results for your test cases.
内容的提问来源于stack exchange,提问作者Gligor Atanasovski

