In django testing I have a Imagefield which is used in many test cases, In order to avoid the repetition I have assigned the Imagefile obj to the global variable
file = open(os.path.join(settings.BASE_DIR, 'logged_out.jpg'), 'rb')
image = {'image':SimpleUploadedFile(name=file.name, content=file.read(), content_type='image/jpeg')}
class FeedFormTest(TestCase):
def setUp(self):
self.user = baker.make(SnetUser)
self.data = {
'post_info':'Test_data',
}
def test_feed_form_is_valid(self):
#Option 1 Not working
#self.data.update(image)
#form = FeedForm(self.data)
#Option 2 Not working
#form = FeedForm(self.data, image)
#Option 3 Working
form = FeedForm(self.data, image) #locally defined image in SetUp method instead globally
print(form.errors)
self.assertTrue(form.is_valid())
When I execute the test it's working only for the image which is defined inside the setUp method of test class. Can you suggest or guide me the method in using the variables effectively without defining it multiple times. As I have other variables too like user credentials