英文:
How to test a python file that contains no functions and has multiple input calls?
问题
以下是您要翻译的内容:
I have the following python file ```two_strings.py```:
str1 = input()
str2 = input()
combined = str1 + "," + str2
print(combined)
I then have a jupyter notebook file called ```test_two_strings.ipynb```:
import sys
import unittest
from unittest.mock import patch
import two_strings
from io import StringIO
sys.path.append('Challenge10/')
class SimpleTestCase(unittest.TestCase):
def test_defs(self):
defs = [v for v in dir(two_strings) if v[0] != '_']
assert 'str1' in defs, f'Definition for str1 not found'
assert 'str2' in defs, f'Definition for str2 not found'
assert 'combined' in defs, f'Definition for combined not found'
def test_inputs_outputs(self):
user_input = [
['a','b'],
['10','sixty six'],
['hello','there']
['once upon a time','in fairyland']
]
expected_output = [
'a,b',
'10,sixty six',
'hello,there',
'once upon a time,in fairyland'
]
for i in range(len(user_input)):
with patch('builtins.input', side_effect=user_input[i]):
output = StringIO()
with patch('sys.stdout', new=output):
two_strings.main()
self.assertEqual(output.getvalue().strip(), expected_output[i])
if name == 'main':
unittest.main(argv=['-v'],verbosity=2, exit=False)
这是您要求的翻译结果。如果您需要任何其他帮助,请随时提问。
<details>
<summary>英文:</summary>
I have the following python file ```two_strings.py```:
str1 = input()
str2 = input()
combined = str1 + "," + str2
print(combined)
I then have a jupyter notebook file called ```test_two_strings.ipynb```:
import sys
import unittest
from unittest.mock import patch
import two_strings
from io import StringIO
sys.path.append('Challenge10/')
class SimpleTestCase(unittest.TestCase):
def test_defs(self):
defs = [v for v in dir(two_strings) if v[0] != '_']
assert 'str1' in defs, f'Definition for str1 not found'
assert 'str2' in defs, f'Definition for str2 not found'
assert 'combined' in defs, f'Definition for combined not found'
def test_inputs_outputs(self):
user_input = [
['a','b'],
['10','sixty six'],
['hello','there']
['once upon a time','in fairyland']
]
expected_output = [
'a,b',
'10,sixty six',
'hello,there',
'once upon a time,in fairyland'
]
for i in range(len(user_input)):
with patch('builtins.input', side_effect=user_input[i]):
output = StringIO()
with patch('sys.stdout', new=output):
two_strings.main()
self.assertEqual(output.getvalue().strip(), expected_output[i])
if name == 'main':
unittest.main(argv=['-v'],verbosity=2, exit=False)
That will test the python file within the notebook file. I am using jupyterlite. However I am unable to figure out exactly how to setup unit tests for testing multiple lines of input() calls and making sure the output is correct. I know the expected output is correct but knowing how janky using input in jupyter notebooks are in jupyterlite, I want to know if this is possible? If so, how?
</details>
# 答案1
**得分**: 0
以下是翻译好的部分:
适当处理`input()`的方法是将其抽象化。将`input()`移到一个函数中,并使其返回一个字符串元组,类似于:
```python
def get_inputs():
return input(), input()
然后,您可以模拟该函数并使其返回硬编码(或测试生成的)字符串,然后您的测试可以用于验证。
一般来说,单元测试不应以任何方式依赖input()
。您应该摆脱这种依赖。
但是,如果您确实必须按原样测试脚本,即使包括input()
,那么您必须自动传递输入给脚本。这意味着您不能import
脚本,而您的替代方案都不是很好。总的来说,我要说的通常会让我拉开一个开发者,教他们如何正确编写代码。这不是一个好主意。尽量不要做我即将写下的事情。
在这里,您有两种选择:exec
和subprocess
。
对于exec
,您基本上是在当前作用域中以字符串形式运行Python代码。因此,您可以自己模拟input
,直接将本地字典修改为您可以监视的东西,并以此方式使事情正常工作。
def test_something(self):
# 模拟 `input()`
new_input = lambda: 'some string'
# 创建一个可以监视的本地字典
new_locals = {'input': new_input}
# 执行要测试的代码
exec(open('two_strings.py').read(), globals(), new_locals)
# 检查创建的本地变量是否包含您想要的值
self.assertEqual(new_locals['combined'], 'some string,some string')
对于subprocess
,代码有点复杂,所以我将其省略,并留给您填写缺失的部分。但是,一般的思路是在自己的子进程中运行脚本,提供输入,然后监视输出。
如果您实际上没有将要测试的结果存储在可以从测试中访问的变量中,那么您应该更喜欢使用subprocess
。例如,如果您的打印不是print(combined)
而是print(str1 + "," + str2)
。
尽管如此,运行这样的测试有点疯狂。
英文:
The proper way to work around usage of input()
is to abstract it away. Move the input()
into a function and have it return a tuple of strings. Something like:
def get_inputs():
return input(), input()
You can then mock out the function and have it return hard-coded (or test-generated) strings that your test then uses for validation.
In general, you should not have a unit test rely on input()
in any way. You want to get rid of that stuff.
If, however, you absolutely must test the script as-is even with input()
, then you'll have to automate passing in input to the script. Which means you can't import
the script and your alternatives are all bad. In general, what I'm saying can be done here would typically cause me to pull a developer aside and teach them how to properly code. This is not a good idea. Do your best to not do what I'm about to write down.
You have kinda-sorta two choices here: exec
and subprocess
.
For exec
, you are basically running python code as a string in the current scope. So, you can do your own poor-man's mock of input
, direct local dictionary modifications to something you can monitor, and get things working like that.
def test_something(self):
# Mock out `input()`
new_input = lambda: 'some string'
# Create a locals dictionary you can monitor
new_locals = {'input': new_input}
# Execute the code you want to test
exec(open('two_strings.py').read(), globals(), new_locals)
# Check the created local variable for the value you want
self.assertEqual(new_locals['combined'], 'some string,some string')
For subprocess
the code is a bit complicated so I'll elide it and leave it as an exercise for you to fill in the blanks. But, the general idea would be to run the script in its own subprocess, feed it input, and then monitor the output.
You should prefer to use subprocess
if you don't actually store the result you want to test in a variable you can access from the test. For example, if your print wasn't print(combined)
but was instead print(str1 + "," + str2)
.
That being said, running a test like this is a bit crazy.
答案2
得分: 0
以下是翻译好的代码部分:
import sys
from io import StringIO
import unittest
challengeFile = 'challenge.py'
fileRunner = open(challengeFile)
code = fileRunner.read()
class TestName(unittest.TestCase):
def get_stdout(self, inputs):
original_stdin = sys.stdin
original_stdout = sys.stdout
test_inputs = inputs
sys.stdin = StringIO('\n'.join(test_inputs))
sys.stdout = StringIO()
exec(code)
script_output = sys.stdout.getvalue()
sys.stdin = original_stdin
sys.stdout = original_stdout
return script_output
def correct(self, test_output, expected_output):
self.assertEqual(str(test_output).strip(), str(expected_output).strip())
def test_challenge_1(self):
self.correct(self.get_stdout(["1 1 1 1 1 1"]), "6")
def test_challenge_2(self):
self.correct(self.get_stdout(["1 5"]), "6")
def test_challenge_3(self):
self.correct(self.get_stdout(["8 11"]), "19")
fileRunner.close()
unittest.main(exit=False)
这部分代码主要是一个Python测试脚本,用于测试以下的代码(注意这里没有函数):
s = input().split()
sList = list(map(lambda x: int(x), s))
print(sum(sList))
英文:
After many days of testing and looking through documentation it is actually quite simple, as pythons sys
module allows individuals to directly interact with stdout and stdin:
import sys
from io import StringIO
import unittest
challengeFile = 'challenge.py'
fileRunner = open(challengeFile)
code = fileRunner.read()
class TestName(unittest.TestCase):
def get_stdout(self, inputs):
original_stdin = sys.stdin
original_stdout = sys.stdout
test_inputs = inputs
sys.stdin = StringIO('\n'.join(test_inputs))
sys.stdout = StringIO()
exec(code)
script_output = sys.stdout.getvalue()
sys.stdin = original_stdin
sys.stdout = original_stdout
return script_output
def correct(self, test_output, expected_output):
self.assertEqual(str(test_output).strip(), str(expected_output).strip())
def test_challenge_1(self):
self.correct(self.get_stdout(["1 1 1 1 1 1"]), "6")
def test_challenge_2(self):
self.correct(self.get_stdout(["1 5"]), "6")
def test_challenge_3(self):
self.correct(self.get_stdout(["8 11"]), "19")
fileRunner.close()
unittest.main(exit=False)
This will test the following code (notice how there are no functions!):
s = input().split()
sList = list(map(lambda x: int(x), s))
print(sum(sList))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论