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.
Related
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
What I'm trying to do
I'm writing a small framework in python using pytest, and as part of the teardown I am taking a screenshot.
Now, I want that screenshot to be named according to the running test, not conftest.py
So, for example, my code right now is:
driver.save_screenshot(os.path.basename(__file__)+'.png')
The problem
This prints the name "conftest.py".
I want to print the calling test name if possible. I am guessing using requests?
You can access the current file path via request.node.fspath. Example:
# conftest.py
import pytest
#pytest.fixture
def currpath(request):
return str(request.node.fspath)
I have a python module with the following layout:
database_build/
database_build/__init__.py
database_build/some_code_file.py
database_build/tests/__init__.py
database_build/tests/test_database_build.py
database_build/tests/test_content/some_assets_here
test_database_build.py has inside of it a class:
class TestDatabaseBuild(unittest.TestCase):
which has a bunch of tests. Those tests import files from the database_build module itself, e.g. from some_code_file import my_class.
I want to be able to run this set of tests from the database_build directory. How do I do that? Everything I have tried from other StackOverflow questions or from the instructions here do not work for me. It just says "Ran 0 tests in 0.000s".
I'm struggling to know where to start with unittest, having read the dive-into-python tutorial and looked at http://pyunit.sourceforge.net/.
I've got a piece of analysis software (call it 'prog.exe') which uses python for its input decks. I've started writing a python module which I'm going to import from that input deck to provide some useful functionality. So, running one of these analyses will go like this:
prog.exe inputdeck.py
where inputdeck.py contains:
from mymodule import mystuff
So how do I set up and run tests on mymodule? Should the above be in a system call in a setUp method of the test, or what?
Ok - solution:
Don't use unittest.main() as that's the command line tool. Instead call the appropriate unittest methods directly as follows:
From the command line run:
prog.exe mytests.py
where mytests.py contains:
import unittest
# ... code to run the analysis which we'll use for the tests ...
# ... test definitions ...
suite = unittest.TestLoader().loadTestsFromTestCase(test_cases)
unittest.TextTestRunner().run(suite)
See example at http://docs.python.org/release/2.6.7/library/unittest.html#unittest.TextTestRunner
Pyunit is a little bit outdated (2001), it is now completely included in python core distribution (http://docs.python.org/library/unittest.html). You should start read this documentation, especially the basic example part.
To test your module you'll have to create a file, let's call it mymodule_test.py and put in it something like this :
import unittest
from mymodule import mystuff
class MyTestCase(unittest.TestCase):
def test_01a(self):
""" test mystuff"""
self.failUnless(mystuff.do_the_right_stuff())
if __name__ == '__main__':
unittest.main()
and run it with python mymodule_test.py
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.