Python Forum
Writing unit test results into a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing unit test results into a text file
#1
I want to write the results of my unit tests into a text file but it does not seem to be working. Here is my code:

I shall be grateful if someone can please help?
unit tests:

Quote:import unittest
import validate_setup
import sys


class TestValidateSetup(unittest.TestCase):

df = validate_setup.reading_excel()

def test_validate_setup_mesh_size(self):
self.assertEqual(TestValidateSetup.df.iloc[0,1], 0.3)

def test_validate_setup_stresses_max_von_mises(self):
self.assertEqual(TestValidateSetup.df.iloc[1,1], 300)

def test_validate_setup_stresses_min_von_mises(self):
self.assertEqual(TestValidateSetup.df.iloc[2,1], 300)

def test_validate_setup_stresses_max_principal(self):
self.assertEqual(TestValidateSetup.df.iloc[3,1], 400)

def test_validate_setup_stresses_min_principal(self):
self.assertEqual(TestValidateSetup.df.iloc[4,1], 50)

Writing to a text file. Here is the problem

Quote:def main(out = sys.stderr, verbosity = 2):
loader = unittest.TestLoader()

suite = loader.loadTestsFromModule(sys.modules[__name__])
unittest.TextTestRunner(out, verbosity = verbosity).run(suite)

if __name__ == '__main__':
with open('testing.out', 'w') as f:
main(f)
Reply
#2
Maybe look at subprocess and some of the methods there, i know you can bring information into your code that way?

Sorry i cant help any more
Reply
#3
No really sure what is wrong as ssame solution is given in few other forums. I'm using Eclipse IDE
Reply
#4
Any help will be gratefully appreciated. I ven tried the below code but the output is not written to the file/no file is created

Quote:import unittest
import validate_setup
import sys



class TestValidateSetup(unittest.TestCase):

df = validate_setup.reading_excel()

def test_validate_setup_mesh_size(self):
self.assertEqual(TestValidateSetup.df.iloc[0,1], 0.3)

def test_validate_setup_stresses_max_von_mises(self):
self.assertEqual(TestValidateSetup.df.iloc[1,1], 300)

def test_validate_setup_stresses_min_von_mises(self):
self.assertEqual(TestValidateSetup.df.iloc[2,1], 300)

def test_validate_setup_stresses_max_principal(self):
self.assertEqual(TestValidateSetup.df.iloc[3,1], 400)

def test_validate_setup_stresses_min_principal(self):
self.assertEqual(TestValidateSetup.df.iloc[4,1], 50)

if __name__ == "__main__":

sys.stdout = open(r'C:\Temp\output.txt', 'w')
unittest.main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unit Testing Set Up and Use RockBlok 2 425 Jan-08-2024, 07:43 PM
Last Post: deanhystad
  Updating sharepoint excel file odd results cubangt 1 822 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 957 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Trying to send file to printer with no results. chob_thomas 2 3,362 Dec-21-2022, 07:12 AM
Last Post: Pedroski55
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,368 Sep-27-2022, 01:38 PM
Last Post: buran
  How to sort .csv file test log which item first fail and paint color SamLiu 24 4,841 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
  Writing into 2 text files from the same function paul18fr 4 1,671 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Writing to json file ebolisa 1 996 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,653 Apr-13-2022, 06:02 AM
Last Post: Paqqno

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020