Use python for automated testing-unit test for Java code (2)-import a third-party jar package
Using Jython to perform unit tests on Java is of course to test the java code developed by the company. This involves importing the third package. How can I import a third-party package? You can use the functions implemented here in the http://blog.csdn.net/powerccna/article/details/37739207 to scan the jar package under the specified directory, and then add it to the sys. path of jython through the sys. path. append () function.
for jar_file in scan_files("/home/jim/java_jar",postfix=".jar"): sys.path.append(jar_file)
Import a third-party package. You must append the package before you can import it. Otherwise, the package cannot be found. Some people say that they want to add the jar package of mysql driver to CLASSPATH at the same time. When I use it, I only need to add the jar package of mysql driver to CLASSPATH, while others are directly added to sys. path.
Note: from org.apache.commons.net. ftp import FTPClient is still a bit different from cpython. FTPClient is a file here. It corresponds to a module in Cpython and the FTPClient class needs to be initialized in Cpython. It should be ftp = FTPClient. FTPClient () Because FTPClient. FTPClient is the real class, But here ftp = FTPClient (), Jython is implemented in java, so follow the java rules.
#! /Usr/bin/env jython # coding = utf-8import sysimport osimport javaimport unittestimport time # scan_files is a function implemented in another place, the implementation method of the function is deleted here, the unit test is clear for jar_file in scan_files ("/home/jim/java_jar", postfix = ". jar "): sys. path. append (jar_file) # print sys. pathfrom com. mysql. jdbc import Driverimport java. SQL. connectionfrom java. SQL import DriverManagerimport org.apache.commons.net. ftp. FTP; from org.apache.commons.net. ftp import FTPClientimport org.apache.commons.net. ftp. FTPReplydef mysql_driver_test (): java. lang. class. forName ('com. mysql. jdbc. driver ') conn = DriverManager. getConnection ("jdbc: mysql: // 192.168.19.21: 3306/mysql", "root", "root"); class FTPClientTest (unittest. testCase): def setUp (self): self. start_time = time. time () print "starting \ n", def test_login (self): ftp = FTPClient () ftp. connect ("192.168.23.117") ftp. login ("root", "root") self. assertEquals (ftp. getReplyCode (), 230) def test_files_list (self): # The reason why I log on to the ftp server again is to keep the independence of each case, ensure not # subsequent tests file_existing = False ftp = FTPClient () ftp will not be affected due to the previous case. connect ("192.168.23.117") ftp. login ("root", "root") if ftp. getReplyCode () = 230: files = ftp. listNames ("/export/home/test") for fi in files: if "python-2.5-sol10-x86-local.gz" in fi: file_existing = True break self. assertEquals (file_existing, True) def tearDown (self): print "cost", time. time ()-self. start_time, "second" print "end" if _ name __= = "_ main _": unittest. main ()Running result:
[Root @ host-192-168-53-21 jythontest] # jython importjar. py
Starting
Cost 0.157999992371 second
End
. Starting
Cost 0.0529999732971 second
End
----------------------------------------------------------------------
Ran 2 tests in 0.215 s
OK