![](/img/trans.png)
[英]Apache Beam Python Unit Testing a ParDo class with_outputs
[英]python: testing the outputs ( … not unit testing ?)
我有一個名為script.py
的python腳本,它需要輸入A
並生成輸出B
A -> script.py -> B
每次添加新功能時,都需要檢查程序是否生成相同的輸出。
我怎樣才能使這種測試類型自動化?
此測試類型的名稱是什么?
是否存在一些用於此測試類型的python模塊?
據我了解,
您想測試那段代碼(單元)是否按預期執行。 那是單元測試。
您可以做的是做一個測試,為script.py
提供輸入( A
),並生成輸出。 然后,您可以只檢查輸出是否匹配。
class OutputTestCase(unittest.TestCase):
def get_output(self, input):
... # You haven't mentioned how "input" is taken or how output is taken.
def test_script(self):
input = ...
expected = ...
output = self.get_output(input)
self.assertEqual(output, expected)
PS:這就是PLY測試其代碼的方式。 而且我很喜歡它!
您可以編寫一個例外腳本來自動執行此操作。
您可以使用pytest
進行測試。 因此,您可以做的是創建一個testing_suite.py
並將您的測試以test_
開頭的名稱test_
,例如:test_B,test_C等
# testing_suite.py
import pytest
def test_B:
proc = subprocess.Popen(['python', 'script.py', 'arg1'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = proc.communicate()[0]
if output == 'B':
print "testB done" # or something else depends on you
def test_C:
....
....
那么您可以運行為:
$> py.test
因此,這應該使用test_
執行所有測試。 請在此處查看pytest doc以獲得測試發現規則 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.