This is my code:
import unittest
from sallad.Puppgift import Kundenssallad
class Test_kundenssallad(unittest.TestCase):
def test_av_objekt(self):
namn = "Grekisksallad"
slutpris = 60
tillval = "gurka"
kundenssallad = Kundenssallad(namn, slutpris, tillval)
self.assertIsInstance(kundenssallad, Kundenssallad)
self.assertEqual(kundenssallad.slutpris, 60)
self.assertEqual(kundenssallad.tillval, "gurka")
if __name__ == "__main__":
unittest.main()
Upon running this code in PyCharm, I get:
Testing started at 11:32 ...
Process finished with exit code 0
I expected the program to print something like:
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
Why do the tests not seem to execute?
You have not told unittest what test suite to run.
pycharm not matching the if name == "main" ?
Try add some debug and print name.
Run it something like this:
if __name__ == "__main__":
unittest.main()
suite = unittest.TestLoader().loadTestsFromTestCase(Test_kundenssallad)
unittest.TextTestRunner(verbosity=2).run(suite)
Related
I am using spyder(3.3.4) to run some python(3.7) unit tests. I have two different scripts that use unittest and unittest.main() open in spyder: test_1.py and test_2.py. Both are saved in the same folder.
If I run test_1.py before ever running test_2.py, I get the expected results; only the tests in test_1.py are run.
If I then run test_2.py, the tests in test_2.py are run, but then the tests in test_1.py are run also. If I restart the Ipython kernel in between running test_1.py and running test_2.py, then only test_2.py runs, as expected. Weirdest of all, if I close test_1.py after running it, the output of test_1.py is still printed when running test_2.py.
Why is this happening?
I am guessing this has do to do with how the __name__ variable is saved in the IPython console or how unittest.main() searches for tests?
Here's the code for my two test scripts:
test_1.py:
import unittest
class TestStuff(unittest.TestCase):
def test_1(self):
print('test 1')
pass
def test_2(self):
print('test 2')
pass
if __name__ == '__main__':
unittest.main()
and test_2.py:
import unittest
class TestOtherStuff(unittest.TestCase):
def test_this(self):
print('this')
pass
def test_that(self):
print('that')
pass
if __name__ == '__main__':
unittest.main()
Thanks!
I have read several posts saying that if you call your unittests with unittest.main() that they should exit with a failure code if they fail. I call my unittests with the command: python -m unittest discover -v. I am using Python 3.6.6. An example unittest could look like this:
from server import app
import unittest
class ServerTestCase(unittest.TestCase):
"""
Unittesting for the server application.
"""
def setUp(self):
"""
Create a test client
"""
self.app = app.test_client()
self.app.testing = True
def tearDown(self):
pass
def test_root_endpoint(self):
"""
Testing the root endpoint
"""
result = self.app.get('/')
self.assertEqual(result.status_code, 200)
def test_health_endpoint(self):
"""
Testing the health endpoint
"""
result = self.app.get('/health')
assert b'UP' in result.data
if __name__ == '__main__':
unittest.main()
Even if one of my tests fails I get this when checking the exit code:
$ echo $?
0
What am I doing wrong?
Did you name the unittest files something like test_*.py? Because that's what discover is looking for. Otherwise no matter your tests, the result will be:
Ran 0 tests in 0.000s
OK
(BTW you don't have to if __name__ ... unittest.main() when using -m unittest discover)
I am trying to run some unittest tests via a Python Invoke library, but my poor knowledge of Python prevents me from doing so.
This is the sample code I have:
my_tests.py
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
def main():
unittest.main()
if __name__ == '__main__':
main()
tasks.py
from invoke import task
#task
def tests(ctx):
main()
#task
def other_task(ctx):
print("This is fine")
def main():
import my_tests
import unittest
unittest.main(module='my_tests')
if __name__ == '__main__':
main()
And this is what I get:
C:\ittle_projects\invoke_unittest>python my_tests.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.002s
OK
C:\ittle_projects\invoke_unittest>python tasks.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
C:\ittle_projects\invoke_unittest>inv tests
E
======================================================================
ERROR: tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module 'my_tests' has no attribute 'tests'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
The tests run fine from my_tests.py and from tasks.py, but when I use invoke stuff breaks.
How can I make it work or where should I look next?
The issue you are running into is that unittest.main() uses the command line arguments your program is called with to determine which tests to run. Since your program is being executed as inv tests, the first argument to your program is tests, so unittest is attempting to run tests for a module name tests which does not exist.
You can get around this by popping the last argument (tests) from the system arguments list:
import sys
from invoke import task
#task
def tests(ctx):
# Pop "tests" off the end of the system arguments
sys.argv.pop()
main()
#task
def other_task(ctx):
print("This is fine")
def main():
import my_tests
import unittest
unittest.main(module='my_tests')
if __name__ == '__main__':
main()
So let's say I have the following:
import unittest
class MyTests(unittest.TestCase):
def test001(self):
print 'This is test001'
def test002(self):
print 'This is test002'
if __name__ == '__main__':
unittest.main()
print 'Done'
And the output is:
>> This is test001
>> This is test002
>> ----------------------------------------------------------------------
>> Ran 2 tests in 0.001s
>> OK
And I was wondering why doesn't get to print 'Done' (or anything that comes after)?
Pass exit=False to the unittest.main() call (documentation):
unittest.main(exit=False)
Here's what I'm getting on the console:
$ python test.py
This is test001
.This is test002
.
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
Done
FYI, under the hood unittest's TestProgram.runTests() calls sys.exit() if the value of exit is True (which is by default):
def runTests(self):
...
if self.exit:
sys.exit(not self.result.wasSuccessful())
I want to have the files of my application under the folder /Files, whereas the test units in /UnitTests, so that I have clearly separated app and test.
To be able to use the same module routes as the mainApp.py, I have created a testController.py in the root folder.
mainApp.py
testController.py
Files
|__init__.py
|Controllers
| blabla.py
| ...
UnitTests
|__init__.py
|test_something.py
So if in test_something.py I want to test one function that is in /Files/Controllers/blabla.py, I try the following:
import unittest
import Files.Controllers.blabla as blabla
class TestMyUnit(unittest.TestCase):
def test_stupid(self):
self.assertTrue(blabla.some_function())
if __name__ == '__main__':
unittest.main()
And then from the file testController.py, I execute the following code:
import TestUnits.test_something as my_test
my_test.unittest.main()
Which outputs no failures, but no tests executed
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
[Finished in 0.3s]
I have tried with a test that has no dependences, and if executed as "main" works, but when called from outside, outputs the same:
import unittest
def tested_unit():
return True
class TestMyUnit(unittest.TestCase):
def test_stupid(self):
self.assertTrue(tested_unit())
if __name__ == '__main__':
unittest.main()
Question: how do I get this to work?
The method unittest.main() looks at all the unittest.TestCase classes present in the context.
So you just need to import your test classes in your testController.py file and call unittest.main() in the context of this file.
So your file testController.py should simply look like this :
import unittest
from UnitTests.test_something import *
unittest.main()
In test_something.py, do this:
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestMyUnit, 'test'))
return suite
In testController.py, do this:
from TestUnits import test_something
def suite():
suite = unittest.TestSuite()
suite.addTest(test_something.suite())
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')
There is a workaround of using subprocess.call() to run tests, like:
import subprocess
args = ["python", "test_something.py"]
subprocess.call(args)