title: Can't unit-test image upload with python
body: <p>I have a function that receives data from a form via POST. I have some textfields and a file field in order to upload an image(.jpg).</p>
  
  <p>The function expects to get the image file from request.files['file']. Being "file" the name of the image form field.</p>
  
  <p>I don't know how to run my function as a test and pass it the request.files[] argument like I have done with the other field values(name, password, mail, phone...)</p>
  
  <p>This is the function I wanna test (app.py):</p>
  
  <pre><code>@app.route('/registering', methods=['GET', 'POST'])
  def registering():
      if request.method == 'POST':
          userCheck = request.form['username']
          userCheck2 = request.form['email']
          userCheck3 = request.form['password']
          userCheck4 = request.form['passwordCheck']
          userCheck5 = request.form['phone']
  
          file = request.files['file']
          if file and allowed_file(file.filename):
  
              newUser = users(userCheck, userCheck2, userCheck5, userCheck3)
              db.session.add(newUser)
              db.session.commit()
              userName = users.query.filter_by(
                      userName=userCheck, userPass=userCheck3).first()
  
              session['logged_in'] = True
              session['user_id'] = userName.id
  
              filename = str(userName.id)
              file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename + ".jpg"))
  
              flash('"Registered Successfully"')
              return redirect(url_for('friendList'))
  
      else:
          return render_template('register.html')
  
  def allowed_file(filename):
      return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
  </code></pre>
  
  <p>Here is my test (app_tests.py):</p>
  
  <pre><code>import os
  import app
  import unittest
  import tempfile
  
  class AppTestCase(unittest.TestCase):
  
      def setUp(self):
          self.db_fd, app.app.config['DATABASE'] = tempfile.mkstemp()
          app.app.config['TESTING'] = True
          self.app = app.app.test_client()
  
      def tearDown(self):
          os.close(self.db_fd)
          os.unlink(app.app.config['DATABASE'])
  
  
      def registering(self, username, email, password, passwordCheck, phone, file):
      return self.app.post('/registering', data=dict(
          username=username, 
          email=email, 
          password=password, 
          passwordCheck=passwordCheck, 
          phone=phone,
          file=file
      ), follow_redirects=True)
  
      def test_registering(self):
          #successfully registered
  
          rv = self.registering('TestUser', 'test@test.com', 'passwordTest', 'passwordTest', '900102030')
          assert 'Registered Successfully' in rv.data
          #existing username/user
          rv = self.registering('Hulda', 'hulda@hulda.com', 'passwordHulda', 'passwordHulda', '900102030')
          assert 'Username Taken' in rv.data
           #no username
          rv = self.registering('', 'santana@santana.com', 'passwordSantana', 'passwordSantana', '900102030')
          assert 'Username required' in rv.data
          #no email
          rv = self.registering('Santana', '', 'passwordSantana', 'passwordSantana', '900102030')
          assert 'Email required' in rv.data
          #no password
          rv = self.registering('Santana', 'santana@santana.com', '', 'passwordSantana', '900102030')
          assert 'Password required' in rv.data
          #no password confirmation
          rv = self.registering('Santana', 'santana@santana.com', 'passwordSantana', '', '900102030')
          assert 'Confirm password' in rv.data
          #no password match
          rv = self.registering('Santana', 'santana@santana.com', 'passwordSantana', 'passwordSsssssntana', '900102030')
          assert 'Retype passwords' in rv.data
          #no phone
          rv = self.registering('Santana', 'santana@santana.com', 'passwordSantana', 'passwordSantana', '')
          assert 'Phone required' in rv.data
  </code></pre>