Wanted to use unittest for some data examples.
import unittest
from peculiar.py import misma_paridad, alterna_paridad
class TestMismaParidad(unittest.TestCase):
def test_misma_paridad(self):
self.assertTrue(misma_paridad(0, 0))
self.assertFalse(misma_paridad(0, 1))
class TestAlternaParidad(unittest.TestCase):
def test_alterna_paridad(self):
self.assertTrue(alterna_paridad(1234))
self.assertFalse(alterna_paridad(1224))
unittest.main()
After several attempts of editing code, still getting:
Ran 0 tests in 0.000s
OK
Can someone tell me where the issue might be?
Adding a blank __init__.py file to the same folder my test was in fixed it for me
Related
I'm working on specific projects where I use python (3.4) scripts as a replacement for bash/batch scripting. I'm trying to make use of unittest module in order to get a quick and easy report.
I'm checking many different things with the script, taking requests and getting correct file from the server, unpacking .zip / .gz files, checking if there's a certain tree structure within the package, checking if there is a correct copyright on certain locations and so on.
For a different project I used NodeJS "Should" module, which was awesome to easily get the report:
it('Should return Tesla for that particular model', function(){
myProps = beApi.getPropertiesSync(Tesla);
myProps.contains('model', 'Tesla').should.be.equal(true, 'but was '+ myProps.get('model').getValue());
});
This is just one of examples.
Now the question, is there similar way to do it in Python?
Also, I am trying to make use of unittest module but it does not run (just yet) and I can't find a simple example that should work. This is what I have so far:
import fileinput
import zipfile
import unittest
class MyTest(unittest.TestCase):
def shall_fail(self):
self.assertEqual(input_doubled(5),25, "Something Wrong")
def main():
print (input_doubled(5))
unittest.main()
pass
def input_doubled(x):
return x*x+2
if __name__ == '__main__':
main()
And my output is, sadly:
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
25
I am writing python selenium tests
if __name__ == "__main__":
unittest.main()
This is my main.py file, I import the tests from a different folder
from tests.AuthorTest import AuthorTest
When I run main.py it gives me a message
python main.py
----------------------------------------------------------------------
Ran 0 tests in 0.000s
for some reason, when I pull the code from git hub I can edit and change existing files but when I add new tests to main.py, it still says Ran 0 tests. Im guessing the main isn't importing it correctly? Been having this problem for a while and I can't seem to fix it.
Appreciate any and all help!
All test functions when using unittest modules should begin with test in your test file.
"A testcase is created by subclassing unittest.TestCase. The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests."
From http://docs.python.org/2/library/unittest.html
class TestSequenceFunctions(unittest.TestCase):
def choice(self):
self.assertTrue(False)
will not show any result but when def choice(self) is renamed to def testchoice(self), unittest now will show that one of the tests failed.
In testsuite/__init__.py, I write this:
import unittest
def suite():
from my_module.testsuite import (
shell_command,
shell_command_on_jinja,
workflow
)
suite = unittest.TestSuite()
suite.addTest(shell_command.suite())
suite.addTest(shell_command_on_jinja.suite())
suite.addTest(workflow.suite())
return suite
In terminal, I can execute the test suite like this:
python3 -m unittest testsuite.suite
However, I don't know how to config the test for Pycharm/InteliJ. I add a configuration under Python's test and set Test to Function, Script to my_module/testsuite/__init__.py, and Function to suiteand then run it. But it doesn't work. Does anyone have ideas about this? Thanks!
It seems that PyCharm doesn't support this now. Please check the following thread.
http://forum.jetbrains.com/thread/PyCharm-1116
I'm not sure of how to get the nose module's __main__ handler to work. I have this at the end of my test module:
if __name__ == "__main__":
import nose
nose.main()
Which gives me:
----------------------------------------------------------------------
Ran 0 tests in 0.002s
OK
but it I run the same thing via the command line, it finds the tests and executes them:
MacBook-Pro:Storage_t meloam$nosetests FileManager_t.py
............E..
======================================================================
ERROR: testStageOutMgrWrapperRealCopy (WMCore_t.Storage_t.FileManager_t.TestFileManager)
----------------------------------------------------------------------
SNIP
----------------------------------------------------------------------
Ran 15 tests in 0.082s
FAILED (errors=1)
I've been playing with passing different arguments to nose.main() but I can't find anything that works. Am I missing something really obvious?
Thanks
For posterity's sake, this is what I use:
if __name__ == '__main__':
import nose
nose.run(argv=[__file__, '--with-doctest', '-vv'])
The --with-doctests will also execute your doctests in the same file.
if __name__ == '__main__':
import nose
nose.run(defaultTest=__name__)
nose.runmodule is the way to go:
if __name__ == '__main__':
import nose
nose.runmodule()
I recommend checking 2 things:
Make sure your Source FILES follow the appropriate naming convention: (detailed in this answer).
I, for instance, had to append "_Test" to all my source files.
Then, all you need is this argument (assuming your main is at the root of the tests):
nose.main(defaultTest="")
I tried with:
nose.run(defaultTest=__name__)
as a previous answer suggested, but for some reason it wasnt working for me. I had to do BOTH things to get it working!
Hope it helps.
EDIT:
By the way, calling with
nose.run()
or
nose.main()
made no discernible difference either.
You need to use nose.core.TestProgram directly by passing it fake command line arguments. That I'm not sure though will if find your tests from the same module as you're using
Few times while browsing tests dir in various Django apps I stumbled across models.py and settings.py files (in django-tagging for example).
But there's no code to be found that syncs test models or applies custom test settings - but tests make use of them just as if django would auto-magically load them. However if I try to run django-tagging's tests: manage.py test tagging, it doesn't do even a single test.
This is exactly what I need right now to test my app, but don't really know how.
So, how does it work?
If you want to run the tests in django-tagging, you can try:
django-admin.py test --settings=tagging.tests.settings
Basically, it uses doctests which are in the tests.py file inside the tests package/directory. The tests use the settings file in that same directory (and specified in the command line to django-admin). For more information see the django documentation on writing doctests.
You mean, "How do I write unit tests in Django?" Check the documentation on testing.
When I've done it, I wrote unit tests in a test/ subdirectory. Make sure the directory has an empty __init__.py file. You may also need a models.py file. Add unit tests that derive from unittest.TestCase (in module unittest). Add the module 'xxxx.test' to your INSTALLED_APPS in settings.py (where 'xxxx' is the base name of your application).
Here's some sample code of mine to get you started:
#!/usr/bin/env python
# http://docs.djangoproject.com/en/dev/topics/testing/
from sys import stderr
import unittest
from django.test.client import Client
from expenses.etl.loader import load_all, load_init
class TestCase(unittest.TestCase):
def setUp(self):
print "setUp"
def testLoading(self):
print "Calling load_init()"
load_init()
print "Calling load_all()"
load_all()
print "Done"
if __name__ == '__main__':
unittest.main()
If you mean, "How do I get data loaded into my unit tests?", then use fixtures, described on the same documentation page.