Rewrite test_filepost.py to be pytest-style
MODIFY
test/test_filepost.py
test/test_filepost.py
-import unittest
+import pytest
from urllib3.filepost import encode_multipart_formdata, iter_fields
from urllib3.fields import RequestField
@@ -8,45 +8,41 @@ from urllib3.packages.six import b, u
BOUNDARY = '!! test boundary !!'
-class TestIterfields(unittest.TestCase):
+class TestIterfields(object):
def test_dict(self):
for fieldname, value in iter_fields(dict(a='b')):
- self.assertEqual((fieldname, value), ('a', 'b'))
+ assert (fieldname, value) == ('a', 'b')
- self.assertEqual(
- list(sorted(iter_fields(dict(a='b', c='d')))),
+ assert (
+ list(sorted(iter_fields(dict(a='b', c='d')))) ==
[('a', 'b'), ('c', 'd')])
def test_tuple_list(self):
for fieldname, value in iter_fields([('a', 'b')]):
- self.assertEqual((fieldname, value), ('a', 'b'))
+ assert (fieldname, value) == ('a', 'b')
- self.assertEqual(
- list(iter_fields([('a', 'b'), ('c', 'd')])),
+ assert (
+ list(iter_fields([('a', 'b'), ('c', 'd')])) ==
[('a', 'b'), ('c', 'd')])
-class TestMultipartEncoding(unittest.TestCase):
+class TestMultipartEncoding(object):
- def test_input_datastructures(self):
- fieldsets = [
+ @pytest.mark.parametrize('fields', [
dict(k='v', k2='v2'),
[('k', 'v'), ('k2', 'v2')],
- ]
-
- for fields in fieldsets:
+ ])
+ def test_input_datastructures(self, fields):
encoded, _ = encode_multipart_formdata(fields, boundary=BOUNDARY)
- self.assertEqual(encoded.count(b(BOUNDARY)), 3)
+ assert encoded.count(b(BOUNDARY)) == 3
- def test_field_encoding(self):
- fieldsets = [
+ @pytest.mark.parametrize('fields', [
[('k', 'v'), ('k2', 'v2')],
[('k', b'v'), (u('k2'), b'v2')],
[('k', b'v'), (u('k2'), 'v2')],
- ]
-
- for fields in fieldsets:
+ ])
+ def test_field_encoding(self, fields):
encoded, content_type = encode_multipart_formdata(fields, boundary=BOUNDARY)
expected = (b'--' + b(BOUNDARY) + b'\r\n'
b'Content-Disposition: form-data; name="k"\r\n'
@@ -58,10 +54,9 @@ class TestMultipartEncoding(unittest.TestCase):
b'v2\r\n'
b'--' + b(BOUNDARY) + b'--\r\n')
- self.assertEqual(encoded, expected, fields)
+ assert encoded == expected
- self.assertEqual(content_type,
- 'multipart/form-data; boundary=' + str(BOUNDARY))
+ assert content_type == 'multipart/form-data; boundary=' + str(BOUNDARY)
def test_filename(self):
fields = [('k', ('somename', b'v'))]
@@ -74,10 +69,9 @@ class TestMultipartEncoding(unittest.TestCase):
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n')
- self.assertEqual(encoded, expected)
+ assert encoded == expected
- self.assertEqual(content_type,
- 'multipart/form-data; boundary=' + str(BOUNDARY))
+ assert content_type == 'multipart/form-data; boundary=' + str(BOUNDARY)
def test_textplain(self):
fields = [('k', ('somefile.txt', b'v'))]
@@ -90,10 +84,9 @@ class TestMultipartEncoding(unittest.TestCase):
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n')
- self.assertEqual(encoded, expected)
+ assert encoded == expected
- self.assertEqual(content_type,
- 'multipart/form-data; boundary=' + str(BOUNDARY))
+ assert content_type == 'multipart/form-data; boundary=' + str(BOUNDARY)
def test_explicit(self):
fields = [('k', ('somefile.txt', b'v', 'image/jpeg'))]
@@ -106,10 +99,9 @@ class TestMultipartEncoding(unittest.TestCase):
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n')
- self.assertEqual(encoded, expected)
+ assert encoded == expected
- self.assertEqual(content_type,
- 'multipart/form-data; boundary=' + str(BOUNDARY))
+ assert content_type == 'multipart/form-data; boundary=' + str(BOUNDARY)
def test_request_fields(self):
fields = [RequestField('k', b'v',
@@ -123,4 +115,4 @@ class TestMultipartEncoding(unittest.TestCase):
b'v\r\n'
b'--' + b(BOUNDARY) + b'--\r\n')
- self.assertEqual(encoded, expected)
+ assert encoded == expected

Rewrite test_filepost.py to be pytest-style