1
import unittest

class TestString(unittest.TestCase):

    def setUp(self):
        self.subject_list = ["Maths","Physics","Chemistry"]

    def test_student_1(self):
        self.assertListEqual(self.subject_list,["Maths","Physics","Chemistry"])
        self.subject_list.remove("Maths")

    def test_student_2(self):
        self.assertListEqual(self.subject_list,["Physics","Chemistry"])

if __name__ == "__main__":
    unittest.main()

Output : one failure and one success.

Does the setUp() loads a copy of every variable defined in it for every test case ?? If yes, How can I use setUp() to access variables globally??

wonder
  • 885
  • 1
  • 18
  • 32
  • 1
    Just keep in mind that it's not a very good test - there is no guarantee that tests will be run in declaration order and then not even have to be run as suite (you can select subset of tests to run). – Łukasz Rogalski Apr 28 '17 at 06:53
  • I don't know about the order of execution but it seems so that the setup method creates a local variable for every test case. So for the above example the order of execution doesn't matter. Yeah for global variables I have to check. Thanks. – wonder Apr 28 '17 at 08:31

1 Answers1

2

setUp run each test method. if you want to run only once, use setUpClass

My English isn't good. so this link helps you

import unittest


class TestString(unittest.TestCase):
    subject_list = ["Maths", "Physics", "Chemistry"]

    def test_student_1(self):
        self.assertListEqual(self.subject_list, ["Maths", "Physics", "Chemistry"])
        self.subject_list.remove("Maths")

    def test_student_2(self):
        self.assertListEqual(self.subject_list, ["Physics", "Chemistry"])


if __name__ == "__main__":
    unittest.main()
Community
  • 1
  • 1
minji
  • 512
  • 4
  • 16
  • So do i need to declare that as kind of a static variable in python??? If yes how is that accessible using self?? – wonder Apr 28 '17 at 08:28