Python interface Automation Test 23: File Upload

Source: Internet
Author: User
Tags sendfile

# Take Zen Road as an example:

First, create a class, the class inside write a login method:

Import requests
Class Loginzentao ():

def __init__ (self, s): # Initialization
Self.s = S # defines a global s

def login (self):
R = Self.s.post () # SELF.S Call Global S
Pass

If __name__== ' __main__ ':
s = requests.session ()
Zentao = Loginzentao (s) # class instantiated as object, passed in parameter s
Zentao.login () # Call the login () method inside the Zentao

Second, upload the image interface:

# File Upload: Content-type:multipart/form-data type
Import requests

Class SendFile ():
def __init__ (self, s):
SELF.S = S

def sendimg (self, Jpgpath, jpgname= ' 1.jpg ', jpgtype= ' image/jpeg '):

# Log in and update cookies
f = open (' 1.jpg ', ' RB ') # Absolute path
Url2 = ' '
BODY = {
' Localurl ': (None, Jpgname),
' Imgfile ': (' 1.jpg ', open (Jpgpath, ' RB '), Jpgtype)
# 1, Absolute path 2, open (' 1.jpg ', ' RB ') 3, Content-type value
}
# When uploading images, no data and JSON, with files
R = Self.s.post (Url2, Files=body) # 1, call Global S, with SELF.S 2, files
Print (R.text)
# uploaded to the server, each time the address is not the same

# Solve Throwing Exceptions
Try
Jpg_url = R.json () [' URL '] # (relative path)
Print (Jpg_url)
Return Jpg_url

Except Exception as msg: # return error message
Print (' picture upload failed, Reason:%s '%msg) # Printing error messages
# raise # Active Throw primitive exception
# Raise # throws the exception content: "..."
Return '

If __name__== ' __main__ ':
s = requests.session ()
From Test.test_009_003_zentaologin import Loginzentao
# Login Method
Login = Loginzentao (s) # Instantiate class Loginzentao as Object
Login.login ()
# Upload Files
Send = SendFile (s) # Instantiate the Class SendFile () as an object
Send.sendimg () # Call the Sendimg method inside the Sendfile ()

Third, write and execute test cases:

Import UnitTest
Import requests
From test.test_009_003_zentaologin import Loginzentao # imported from folder. File name Loginzentao class
From test.test_009_002_sendfile import Sendfile # imported from folder. File name Sendfile class
Import OS

# Get File path
Parpath = Os.path.dirname (Os.path.dirname (Os.path.realpath (__file__))) # Locate the current folder
Jpgpath = Os.path.join (Parpath, ' testdata ', ' 1.jpg ') # "TestData": the folder name where the file is stored, "1.jpg": File name


Class Testsendfile (UnitTest. TestCase):

@classmethod
def setupclass (CLS):
CLS.S = Requests.session ()
# Instantiate the called Class
Cls.login = Loginzentao (CLS.S) # Instantiate class Loginzentao as Object
Cls.send = SendFile (CLS.S) # Instantiate the Class SendFile () as an object

def test_01 (self):
# Upload files: First step, login
Self.login.login ()
# Step two, pass the file
result = SELF.SEND.SENDIMG (Jpgpath)
# Step Three: Assert: The returned result contains '. jpg '
Self.assertin ('. jpg ', result)

# # # Login method
# login = Loginzentao (s) # Instantiate class Loginzentao as Object
# Login.login ()
# # Upload Files
# send = SendFile (s) # Instantiate the Class SendFile () as an object
# send.sendimg () # Call the Sendimg method inside the Sendfile ()

Python interface Automation Test 23: File Upload

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.