Right way to write Unit-Tests in module?
Question: I want to write tests for my main file,`calc.py`, with unittest in module
file, `MyTests.py`.

Here is my main python file, `calc.py`:

    
    
    import myTests
    
    def first(x):
        return x**2
    
    def second(x):
        return x**3
    
    def main():
        one = first(5)
        two = second(5)
    
    if __name__ == "__main__":
        main()
        try:
            myTests.unittest.main()
        except SystemExit:
            pass
    

And here is my `MyTests.py` file:

    
    
    import unittest
    import calc
    
    class TestSequenceFunctions(unittest.TestCase):
        def setUp(self):
            self.testInput = 10
    
        def test_first(self):
            output = calc.first(self.testInput)
            correct = 100
            assert(output == correct)
    
        def test_second(self):
            output = calc.second(self.testInput)
            correct = 1000
            assert(output == correct)
    

When i run my `calc.py`, i get the following output:

    
    
    ----------------------------------------------------------------------
    Ran 0 tests in 0.000s
    
    OK
    

Why does unittest prints me that "Ran **0** test"?  
And what is the right way to write unittest in module?


Answer: `unittests.main()` looks for `TestCase` instances _in the current module_.
Your module has no such testcases; it only has a `myTests` global.

Best practice is to run the tests _themselves_. Add the `__main__` section
there to the `myTests.py` file:

    
    
    import unittest
    import calc
    
    class TestSequenceFunctions(unittest.TestCase):
        def setUp(self):
            self.testInput = 10
    
        def test_first(self):
            output = calc.first(self.testInput)
            correct = 100
            assert(output == correct)
    
        def test_second(self):
            output = calc.second(self.testInput)
            correct = 1000
            assert(output == correct)
    
    if __name__ == '__main__':
        unittest.main()
    

and run `python myTests.py` instead.

Alternatively, pass in the imported `myTests` module into `unittest.main()`.
You may want to move the `import myTests` line _down_ into `__main__`, because
you have a circular import too. That is fine in your case, `myTests` doesn't
use any globals from `calc` outside of the test cases, but it is better to be
explicit about this.

    
    
    if __name__ == "__main__":
        main()
        try:
            import myTests
            myTests.unittest.main(myTests)
        except SystemExit:
            pass
    

