From 572e0c7407a467bf26c07b400d9eb1769b312cb9 Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Sat, 12 Sep 2020 09:03:06 +0200 Subject: [PATCH] test cases: fix generation of random test files --- test/test.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/test/test.py b/test/test.py index 9c12c9f..04f8961 100644 --- a/test/test.py +++ b/test/test.py @@ -8,6 +8,13 @@ import threading import time import requests import re +import random + + +def gen_random_file(fname, size=1024): + with open(fname, 'wb') as fh: + for i in range(size): + fh.write(chr(random.randint(0, 255)).encode('utf-8')) class FormConfigTestCase(unittest.TestCase): @@ -360,10 +367,7 @@ class WebAppTest(unittest.TestCase): "select": "option_a", } - import random - with open('data.csv', 'w') as fh: - for i in range(1024): - fh.write(chr(random.randint(0, 255))) + gen_random_file('data.csv') with open('data.csv', 'rb') as fh: files = {'file': fh} @@ -395,10 +399,7 @@ class WebAppTest(unittest.TestCase): "select": "invalidvalue", } - import random - with open('data.txt', 'w') as fh: - for i in range(1024): - fh.write(chr(random.randint(0, 255))) + gen_random_file('data.txt') with open('data.txt', 'rb') as fh: files = {'file': fh} @@ -435,10 +436,7 @@ class WebAppTest(unittest.TestCase): "select": "option_b", } - import random - with open('data.txt', 'w') as fh: - for i in range(1024): - fh.write(chr(random.randint(0, 255))) + gen_random_file('data.txt') with open ('data.txt', 'rb') as fh: files = {'file': fh} @@ -480,9 +478,7 @@ class WebAppTest(unittest.TestCase): self.assertIn('string=', r.text) def testUpload(self): - import random - with open('data.raw', 'w') as fh: - fh.write(chr(random.randint(0, 255))) + gen_random_file('data.raw') data = { "form_name": "upload"