Use python for automated testing-Perform unit testing on Java code (2)-import third-party jar packages and pythonjar
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
I am a beginner in Python and want to develop an automated mobile phone test script. I want to know a few questions.
1. Can Python be automatically tested on mobile phones? Yes.
2. Which tutorial will be better if you want to learn? First, learn automated testing, then learn python, and then learn it with instances. For more information, see wenku.baidu.com/...1.html.
3. What are the main modules to be learned?
In the world of Python, there is an open-source framework Splinter that can simulate the behavior of browsers very well (in a sense, it can also be said that it is a human access click behavior ). Splinter provides a variety of APIs to obtain page information to determine the results of the current behavior
4. if you understand this line, I will add additional points .. Multiple
Learn more about all aspects of testing. python is just a tool. The theoretical knowledge of testing is very important.
Teach python Automated Testing
This problem is quite broad. First, we recommend that you use 2.5 for the current version. If you want to learn how to get started by using python script testing, I don't know how to get started. There are many points in automated testing, it includes gui, functions, performance, and other aspects, and different software applications are also applied to different packages, such as automated Web page testing, you can directly use the python script to operate the ie kernel browser for testing. Then we can use the pamie module for testing... for example, for automated gui testing, pil, win32, and other modules may be used for operations...