从Java错误运行Python测试
我试图看看是否可以从一个地方同时运行PyTest套件和JUnit测试套件。 我创建了几个简单的项目,作为我正在处理的场景的模型: 项目1=具有可测试结果的Python项目。代码:从Java错误运行Python测试,java,python,eclipse,unit-testing,Java,Python,Eclipse,Unit Testing,我试图看看是否可以从一个地方同时运行PyTest套件和JUnit测试套件。 我创建了几个简单的项目,作为我正在处理的场景的模型: 项目1=具有可测试结果的Python项目。代码: def add(self): a = 1 b = 2 if (a + b) == 3: return True else: return False 项目2=带有pyTest模块的Python项目,该模块测试项目1。代码: import uni
def add(self):
a = 1
b = 2
if (a + b) == 3:
return True
else:
return False
项目2=带有pyTest模块的Python项目,该模块测试项目1。代码:
import unittest
from program import *
class Test(unittest.TestCase):
def testProgram(self):
self.assertTrue(add(self), 'The actual answer and the expected answer do not match')
self.runTests()
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testprogram']
unittest.main()
Project3=具有JUnit类的Java项目,该类使用FileInputStream从Project2复制并运行测试模块。代码:
package project4;
import java.io.File;
import java.io.FileInputStream;
import org.junit.Test;
import org.python.util.PythonInterpreter;
public class MTS2 {
@Test
public void test() throws Exception {
PythonInterpreter interp = new PythonInterpreter();
File src = new File(
"C:\\Users\\name\\workspaces\\project\\Workspace\\project2\\src\\TestProgram.py");
FileInputStream is = new FileInputStream(src);
interp.execfile(is);
is.close();
}
}
项目1和2作为参考项目在项目3中
项目3类工程。当我添加到项目2中的assertTrue失败消息出现时,它肯定正在运行项目2。然而,即使测试应该通过,PyUnit测试也是错误的
测试应通过时的控制台错误消息:
E
======================================================================
ERROR: testProgram (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<iostream>", line 15, in testProgram
AttributeError: 'Test' object has no attribute 'runTests'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
测试失败时的控制台错误消息:
======================================================================
FAIL: testProgram (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<iostream>", line 14, in testProgram
AssertionError: The actual answer and the expected answer do not match
----------------------------------------------------------------------
Ran 1 test in 0.010s
FAILED (failures=1)
如果您能帮我找出问题所在,或者用更好的方式从Java运行Python测试,我们将不胜感激。删除self.runTests?这样就可以去掉AttributeError:“test”对象没有属性“runTests”,但当测试通过时,我仍然会遇到一个错误。
======================================================================
FAIL: testProgram (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<iostream>", line 14, in testProgram
AssertionError: The actual answer and the expected answer do not match
----------------------------------------------------------------------
Ran 1 test in 0.010s
FAILED (failures=1)
============================= ERRORS =============================
Traceback (most recent call last):
File "C:\Users\name\Workspaces\project\Workspace\project2\src\TestProgram.py", line 14, in testprogram
self.assertTrue(add(self), 'The actual answer and the expected answer do not match')
AssertionError: The actual answer and the expected answer do not match