Discussion on interface test Framework--python Series 1

Source: Internet
Author: User
Tags python script


Click "Blue Name" under the heading to quickly follow

Insist is to share, handling is knowledge, the diagram is everyone's progress, no fee training, no wasted blowing water, like on the attention, forwarding (free to help more partners) to exchange, want to know the knowledge please leave a message, to bring you more value, is the direction we look forward to, there is more interest in the Welcome to learn, our subscription number, Contact information is as follows:

More books, please look forward to

Background notes

We know that automation testing includes UI Automation, interface automation, Unit automation, where interface automation testing can be said to be cost-effective, we do not talk about how to conduct interface Automation test today, but to see the interface testing framework of the mystery. Small strange lead everyone together simple and easy to complete the interface test framework, only need to complete 6 jobs, just fine. These courses are in-house through the practice, 0 basic grasp of the key to see if you action up, I will be divided into 6 times, share out, want to learn to look down.

1 Job Content

Title: Read the contents of the A.txt (multi-line) file and write it to B.txt

2 Related knowledge points

In fact, the whole only needs 4 steps, as follows:


We also provide some summary knowledge of our colleagues:One, python in the file, folder operations often used in the OS module and Shutil module common methods.

1. Get the current working directory, that is, the directory path of the current Python script work: OS.GETCWD ()

2. Return all files and directories under the specified directory name: Os.listdir ()

3. function to delete a file: Os.remove ()

4. Delete multiple directories: Os.removedirs (r "C:\python")

5. Verify that the given path is a file: Os.path.isfile ()

6. Verify that the given path is a directory: Os.path.isdir ()

7. Determine if it is an absolute path: Os.path.isabs ()

8. Verify that the given path is really saved: os.path.exists ()

9. Returns the directory name and file name of a path: Os.path.split ()

Example:

Os.path.split ('/home/swaroop/byte/code/poem.txt ') Result: ('/home/swaroop/byte/code ', ' poem.txt ')

10. Detach extension: Os.path.splitext ()

11. Get Path name: Os.path.dirname ()

12. Get File Name: Os.path.basename ()

13. Run the shell command: Os.system ()

14. Read and SET environment variables: os.getenv () and os.putenv ()

15. Give the line terminator used by the current platform: os.linesep Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '

16. Indicate the platform you are using: Os.name for Windows, it is ' NT ', and for Linux/unix users, it is ' POSIX '

17. Renaming: Os.rename (old, new)

18. Create a multilevel directory: Os.makedirs (r "C:\python\test")

19. Create a single directory: Os.mkdir ("Test")

20. Get File attributes: Os.stat (file)

21. Modify file permissions and timestamps: Os.chmod (file)

22. Terminate the current process: Os.exit ()

23. Get File Size: os.path.getsize (filename)

Second, the document operation Method Daquan

1.os.mknod ("test.txt") create an empty file This method does not apply under Windows

2.FP =open ("Test.txt", W) open a file directly and create a file if the file does not exist

3. About open mode:

W: Open in write mode,

A: Open in Append mode (starting with EOF and creating a new file if necessary)

r+: Open in read-write mode

w+: Open in read/write mode (see W)

A +: Open in read/write mode (see a)

RB: Open in binary read mode

WB: Opens in binary write mode (see W)

AB: Open in binary append mode (see a)

rb+: Open in binary read/write mode (see r+)

wb+: Open in binary read/write mode (see w+)

ab+: Open in binary read/write mode (see A +)

Fp.read ([size]) #size为读取的长度, in bytes

Fp.readline ([size]) #读一行, if size is defined, it is possible to return only part of a row

Fp.readlines ([size]) #把文件每一行作为一个list的一个成员 and returns the list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size is the total length of the read content, which means that it may be read only to a portion of the file.

Fp.write (str) #把str写到文件中, write () does not add a newline character after Str

Fp.writelines (seq) #把seq的内容全部写到文件中 (multi-line write-once). This function simply writes faithfully and does not add anything behind each line.

Fp.close () #关闭文件. Python will automatically close files after a file is not used, but this feature is not guaranteed and it is best to develop a habit of shutting them down. If a file is closed and then manipulated, it generates VALUEERROR

Fp.flush () #把缓冲区的内容写入硬盘

Fp.fileno () #返回一个长整型的 "file label"

Fp.isatty () #文件是否是一个终端设备文件 (on UNIX systems)

Fp.tell () #返回文件操作标记的当前位置, starting with the origin of the file

Fp.next () #返回下一行 and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal.

Fp.seek (Offset[,whence]) #将文件打操作标记移到offset的位置. This offset is generally calculated relative to the beginning of the file and is generally a positive number. However, if the whence parameter is provided, whence can be calculated from scratch for 0, and 1 for the current position as its origin. 2 means that the end of the file is calculated as the origin. Note that if the file is opened in a or a + mode, the file action tag is automatically returned to the end of the file each time the write operation is made.

Fp.truncate ([size]) #把文件裁成规定的大小, the default is the location that is cropped to the current file action tag. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add.

Three, directory operation method Daquan

1. Create a Directory

Os.mkdir ("file")

2. Copy the file:

Shutil.copyfile ("Oldfile", "NewFile") #oldfile和newfile都只能是文件

Shutil.copy ("Oldfile", "NewFile") #oldfile只能是文件夹, NewFile can be a file, or it can be a destination directory

3. Copy the folder:

4.shutil.copytree ("Olddir", "Newdir") #olddir和newdir都只能是目录, and newdir must not exist

5. Renaming files (directories)

Os.rename ("Oldname", "NewName") #文件或目录都是使用这条命令

6. Moving Files (directories)

Shutil.move ("Oldpos", "Newpos")

7. deleting files

Os.remove ("file")

8. Deleting a directory

Os.rmdir ("dir") #只能删除空目录

Shutil.rmtree ("dir") #空目录, contents of the directory can be deleted

9. Converting a directory

Os.chdir ("path") #换路径

Note that write files are commonly used with write () and writelines () two functions, the difference is that

File.write (str): Writes the string str to a file

File.writelines (seq): Writes the contents of a sequence seq to a file

Both functions write data only, do not write line breaks, and if you need to wrap, manually add ' \ n ' to the end of the Str written:

NewLine characters are defined differently in each operating system, Windows line break is ' \ r \ n ', Unix/linux's newline character is ' \ n ', and Mac's line break is ' \ R ';

In Python, the line break is uniformly processed, defined as ' \ n ', when written in text mode, and if it is a Windows system, Python will automatically convert \ n to a \r\n,mac system;

Default read-write file, open operation is open in text mode: F = open (R ' D:\Python27\pro\123.bak ', ' W ')

If open in binary mode, specify parameter B:f = open (R ' D:\Python27\pro\123.bak ', ' RB ')

3 Reference Example one

# Encoding:utf-8

‘‘‘

Created on 2015-12-24

@author: Night, Little Freak.

‘‘‘

Import OS

Read content in #从文本 (TXT)

def readdatafromtxt (path):

Contents= ""

If Os.path.exists (path):

Try

Fr=open (path, ' R ')

Contents=fr.read ()

Fr.close ()

Except IOError:

Print "Prompt: A file does not exist, or read an exception!" "

Else

Print "Hint: File" +path+ "does not exist, please enter the correct path! "

Return contents

In #将内容写入到指定文件 (TXT)

def writedatatotxt (path,mytxt):

If Os.path.exists (path):

Try

Fw=open (Path, ' A + ')

Fw.write (mytxt+ "\ n")

Fw.close ()

Except IOError:

Print "prompt: B file does not exist, or read an exception!" "

Else

Print "Hint: File" +path+ "does not exist, create the file! "

Fw=open (Path, "A +")

Fw.write (mytxt+ "\ n")

Fw.close ()

#将A文件的内容写入到B文件中

def writeafile2bfile (Apath,bpath):

Mytxt=readdatafromtxt (Apath)

If Mytxt: #调用方法读取A文件 (TXT) content

Print "a" content: \ n "+mytxt

Writedatatotxt (bpath,mytxt) #调用方法往B文件 (TXT) write content

Mytxtb=readdatafromtxt (Bpath)

Content in print "B: \ n" +mytxtb

Else

Print "Hint: content in a is empty: \ n"

#主函数

if __name__ = = ' __main__ ':

CURRENTPATH=OS.GETCWD () #获取当前路径

Apath =currentpath+ "\\A.txt" #拼接A文件的路径

Bpath =currentpath+ "\\B.txt"

Writeafile2bfile (Apath,bpath)

Pass

4 Reference Example two

#-*-Coding:utf-8-*-

File (' B.txt ', ' WB '). Write (File (' A.txt ', ' RB '). Read ())

To this, our first homework, has always needed the knowledge point and the reference sample all give out, the rest rely on everyone, action up!

Recommended articles

Great history sharing, catalogue list

Fiddler record JMeter script, dry share

Tips for using JMeter, essays (1)

Tips for using JMeter, essays (2)

Tips for using JMeter, essays (3)

Tips for using JMeter, essays (4)

Tips for using JMeter, essays (5)

Tips for using JMeter, essays (6)

Tips for using JMeter, essays (7)

Tips for using JMeter, essays (8)

Tips for using JMeter, essays (9)

JMeter interface Automation, you dare to think, I dare to play

Interface Test Combat--SOAPUI Pro5.1.2

Test the way, talk about ideas, talk about means

Mobile Performance Test Tool iTest4.1

Mobile Test Tool Moonlight release of first version

Automated testing, recommended part of a series of courses

Mid-Autumn ceremony: test, will become wise

What are you going to do in the future as a test?

↓↓↓ click on "Read the original" "See More Information"

Discussion on interface test Framework--python Series 1

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.