0

I need to create a unit test class with a function so i can call the function when some event is triggered. I am using the below method but testcases are not executing

gp = r"somefile"

def MyFunc():


if os.path.exists(gp):
    print("yes")

    class First__Test_Cases(unittest.TestCase):

        def test_001(self):
            print("1")


        def test__002(self):
            print("2")


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

    First__Test_Cases()
else:
    print("fail")

MyFunc()

output - Ran 0 tests in 0.000s

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 3
    What is actually the reason, why you need to create this test class in a function? This looks like a design issue for me. Knowing what is your reason may help others to come up with a more elegant solution – DiKorsch May 18 '21 at 07:06
  • 2
    This absolutely sounds https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem ... no matter what problem you intend to solve this way: dont do it. – GhostCat May 18 '21 at 07:27
  • Now with your changes in the code, I am honestly more confused (also because of your missing indentation for the function's body)... Just so I understand your intention correctly: you want to run the test cases only if a certain file exists, otherwise, they should fail (or should they be ignored?) – DiKorsch May 20 '21 at 06:34

2 Answers2

1

Remove MyFunc() and global parts, it should only contain class and main

mytestfile.py

import unittest

class First_Test_Cases(unittest.TestCase):

    def test_001(self):
        Pass

    def test__002(self):
        Pass

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

Then run

python mytestfile.py

And all tests in the class will be executed:

...
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

You can read more and see more examples in the documentation

If you need to have a function call the test, you should do that in a separate file. Check this post: Run unittests from a different file

Boefst
  • 31
  • 5
  • Thanks ... but this is not i am looking for as i have mentioned in question i need to place unit test class inside function as i need to call it when some event is triggered –  May 18 '21 at 07:20
  • 2
    But then you could take the solution of @Boefst and create a process, that runs the tests when your event is triggered – DiKorsch May 18 '21 at 07:23
0

According to what I understood from your change in code, this is your use case:

Tests should be run only if a certain file exists. Otherwise, they should be skipped.

For this use case, I would suggest the following solution:

import os
import unittest

gp = "some_file.txt"
msg = "file {} does not exist".format(gp)

class First_Test_Cases(unittest.TestCase):

    @unittest.skipUnless(os.path.exists(gp), msg)
    def test_001(self):
        pass

    @unittest.skipUnless(os.path.exists(gp), msg)
    def test_002(self):
        pass

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

The output would be the following if the file does not exist:

ss
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK (skipped=2)

and this one, if it exists:

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

In case if you want your tests to fail, just change the code in this way:

import os
import unittest

gp = "some_file.txt"
msg = "file {} does not exist".format(gp)

class First_Test_Cases(unittest.TestCase):

    def test_001(self):
        self.assertTrue(os.path.exists(gp), msg) # add this line
        # your code

    def test_002(self):
        self.assertTrue(os.path.exists(gp), msg) # add this line
        # your code

Then, the output would be the following:

FF
======================================================================
FAIL: test_001 (__main__.First_Test_Cases)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test.py", line 11, in test_001
    self.assertTrue(os.path.exists(gp), msg)
AssertionError: file some_file.txt does not exist

======================================================================
FAIL: test_002 (__main__.First_Test_Cases)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test.py", line 15, in test_002
    self.assertTrue(os.path.exists(gp), msg)
AssertionError: file some_file.txt does not exist

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (failures=2)
DiKorsch
  • 1,240
  • 9
  • 20
  • Hi Bloody thanks for reply the main point in my question is whole Class should be inside a function & when i call the function the test cases inside class should execute –  May 20 '21 at 10:21