Automated test iOS Python project

As a developer, in order to ensure robust own code, write unit tests are an essential part, but the most straightforward is to manually run every day all over the case. So what can help us solve these cumbersome to operate it, we should think of automated test scripts, yes, we can make use of fully automated test scripts to complete, here are scripts to automate the process every day I columns:

  • 1, pullgit repository of the latest code locally.
  • 2, and then packaged into App.
  • 3, mounted on the simulator.
  • 4, the App operation, unit testing, the test data generated and stored locally.
  • 5, the script reads the test data, a message is sent to the relevant personnel.

When these fully automated, can greatly reduce the maintenance costs of developers, even if there are new items each module, increasing the test case on the line, the following will introduce automated test this five-step how to perform specific, the entire script is using Python write little code function is also very simple, but this can help us to complete the basic automated testing, this is the power of the script, select Pyhton purely personal preference, recently also learn Python, of course, what the end-use look at your own language.

python execute shell commands to complete the test

First confirmed installed on the machine gitand python.
Script to determine whether there is a local project, use the command does not exist git clone ..., there is use the command git pull ....
These commands can be used in the Linux scripts to complete, python's os.popen()method is that you can execute shell commands on Linux.
For example: Add the following code to a test.py file, and then executed on the terminal python test.pycommand you will see, under your current directory is downloading my blog up.

import os

os.popen('git clone https://github.com/leopardpan/leopardpan.github.io.git')   

git pull. . . Update the code is the same.

The next package, install, run shell commands are executed using python

The project packaged into iOS App, following Demothe name of the project

  • os.popen(‘xcodebuild -project Demo.xcodeproj -target Demo -configuration Debug -sdk iphonesimulator’)

After this line of script is complete, you will find that the same will generate a buildfolder.
Debug Debug mode parameter indicates now is, if Xcode Release into the inside, where the need to change Release.
xcodebuild command is part of Xcode Command Line Tools. By calling this command, you can compile iOS engineering, packaging and signing process. You can use xcodebuild -help to look at which specific functions.

Open the iOS simulator, where you are running iPhone 6 Plusyou can also replace other types of simulators

  • os.popen(‘xcrun instruments -w “iPhone 6 Plus”’)

To just pack generated App installed on the simulator
must first uninstall before installing App, or you always run that is initially installed, will not be covered until later installed, uninstall App

  • os.popen(‘xcrun simctl uninstall booted com.test.Demo’)

then later booted is Bundle Identifier, I was com.test.Demo, and then install App

  • os.popen(‘xcrun simctl install booted build/Debug-iphonesimulator/Demo.app ‘)

Contact booted .app followed the path, I packed time is Debug, so the folder name is Debug-iphonesimulator.

App runs in the simulator

  • os.popen(‘xcrun simctl launch booted com.test.Demo’)

contact booted followed Bundle Identifier, I was com.test.Demo.

So far, you will find that your project has been up and running, you can start the next project in Debug mode to execute unit tests and save the test data corresponding to the local as data.json. Then read using python script file of test data, and ultimately sent to the relevant personnel to use e-mail, pyhton read data very simple line of code on the line

  • data = open(‘data.json’).read()

data which is json string for simple script, I directly to json format turned into a string type when stored.

Send e-mail python

I am using SMTP mail sent, sending SMTP mail protocol, Python built-in support for SMTP, you can send plain text messages, HTML messages, and e-mail with attachments.

Python has support for SMTP email smtplib and two modules, responsible for the configuration email message, smtplib responsible for sending e-mail, specific code as follows:

from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib

def format_addr(self,s):
    name, addr = parseaddr(s)
    return formataddr(( 
        Header(name, 'utf-8').encode(), 
        addr.encode('utf-8') if isinstance(addr, unicode) else addr))

def send_mail(self, mail, message, title):
	from_addr = '[email protected]'
	password = ''
	to_addr = mail
	smtp_server = 'smtp.163.com'

	msg = MIMEText(message, 'plain', 'utf-8')
	msg['From'] = self.format_addr(u'自动化测试邮件 <%s>' % from_addr)
	msg['To'] = self.format_addr(u'管理员 <%s>' % to_addr)
	msg['Subject'] = Header(title, 'utf-8').encode()

	server = smtplib.SMTP(smtp_server, 25)
	server.set_debuglevel(1)
	server.login(from_addr, password)
	server.sendmail(from_addr, [to_addr], msg.as_string())
	server.quit()

send_mail('[email protected]','正文','标题')

from_addr is the email address of the sender, password is the password you entered when opening the SMTP
smtp_server is smtp service, if you from_addr is gamil.com, then they would have written smtp_server = 'smtp.gmail.com' up.

Methods send_mail (self, mail, message, title): There are four parameters, the first not pass the second parameter is the recipient's mailbox, and the third is the body of the message, and the fourth is the title of the message, method call format: send_mail('[email protected]','正文','标题')

Note: The sender's mailbox must open SMTP functionality for the job, otherwise it will error

163 SMTP opened, you need to log NetEase mailbox, then click on the top of the settings will appear POP3/SMTP/IMAP, then click, select the check is turned on, this time you need to enter a password, remember this password is the code above password, if you are done, then , you copy the above code appears to be modified to your own mailbox, use pyhton run it.

The above combination of several processes can be achieved with a simple automated testing, and if you have any suggestions and comments are welcome discussion.

Reference links: the SMTP to send mail


Reproduced please specify: Panbai Xin's blog >> Click to read the original text

Original: Big Box  Python automated test iOS project


Guess you like

Origin www.cnblogs.com/chinatrump/p/11614886.html