Use Python to do your own automated tests--do unit tests on Java Code (2)-Import third-party jar packages

Source: Internet
Author: User

Use Jython to do unit testing of Java, of course, in order to test the development of Java code. This involves the introduction of a third package, how to import a third party package? You can use the implemented function http://blog.csdn.net/powerccna/article/details/37739207 here to scan the jar package under the specified folder, and then pass the Sys.path.append () The function is added to the Sys.path of Jython.

For Jar_file in Scan_files ("/home/jim/java_jar", postfix= ". Jar"):    sys.path.append (Jar_file)

Import third party package, must first append come in after the talent enough import, otherwise will be reported not to find the package. Some people say in the use of the same time to add to the classpath inside. When I use it. Just MySQL driver this jar package needs to be added to the classpath inside, the other directly added to the sys.path inside can be used.

Here is a demo sample third party package is commons-net-3.3, can always download the jar package here, http://commons.apache.org/proper/commons-net/download_net.cgi


Note here from org.apache.commons.net.ftp import ftpclient, or cpython a bit different, ftpclient here is a file, the corresponding CPython inside called a module, In CPython to initialize the FtpClient class. It should be ftp=ftpclient.ftpclient (), because Ftpclient.ftpclient is the real class. But this is ftp=ftpclient (), Jython is implemented in Java. Adhere to the rules of Java more points.

#!/usr/bin/env jython#coding=utf-8import sysimport osimport javaimport unittestimport Time#scan_files is a function implemented in another place, Here the implementation of the function is removed for everyone to see the unit test this 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 (SE LF): Ftp=ftpclient () ftp.connect ("192.168.23.117") ftp.login ("root", "root") self.assertequals ( Ftp.getreplycode (), Def test_files_list (self): #这里之所以又一次登录ftpserver, the results of the preceding case are not reused. is to maintain the independence of each case and ensure that it does not #由于前面case的原因影响后继的測试 file_existing=false ftp=ftpclient () ftp.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 ()
Execution Result:

[Email protected] jythontest]# Jython importjar.py
Starting
Cost 0.157999992371 Second
End
. Starting
Cost 0.0529999732971 Second
End
----------------------------------------------------------------------
Ran 2 Tests in 0.215s

Ok

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Use Python to do your own automated tests--do unit tests on Java Code (2)-Import third-party jar packages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.