Python Forum

Full Version: Writing unit test results into a text file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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
No really sure what is wrong as ssame solution is given in few other forums. I'm using Eclipse IDE
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()