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
Related
I wrote a python script to do all my tests automatically for me, and generate a HTML report. I discovered discover for unittests the other day which lets me run all the unittests in a given directory without explicitly naming them, and I'd really like to be able to do my doctests the same way, rather than having to import each module explicitly.
I found some info on how to do this at https://docs.python.org/2/library/doctest.html but didn't really get it. Could you please help me with using discover with my doctests?
Python test discovery with doctests, coverage and parallelism is related, but still doesn't answer my question.
coverage_module
import coverage
import doctest
import unittest
import os
# import test_module
import my_module
cov = coverage.Coverage()
cov.start()
# running doctest by explicity naming the module
doctest.testmod(my_module)
# running unittests by just specifying the folder to look into
testLoad = unittest.TestLoader()
testSuite = testLoad.discover(start_dir=os.getcwd())
runner = unittest.TextTestRunner()
runner.run(testSuite)
cov.stop()
cov.save()
cov.html_report()
print "tests completed"
test_module
import unittest
import doctest
from my_module import My_Class
class My_Class_Tests(unittest.TestCase):
def setUp(self):
# setup variables
def test_1(self):
# test code
# The bit that should load up the doctests? What's loader, tests, and ignore though?
# Is this in the right place?
def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(module_with_doctests))
return tests
if __name__ == '__main__':
unittest.main()
Lets figure out what's happening there
1) unittest.discovery
It has no clue of doctests as doctests is a different framework.
So unittest isn't supposed to discover doctests out of the box.
That means you'll need to glue them together by hand
2) doctest
It's essentially a separate framework although it has some glueing classes to convert doctests into unittest-like TestCases.
https://docs.python.org/2.7/library/doctest.html#doctest.DocTestSuite
3) discover
Didn't get what discover you mean, I suppose it's
python -m unittest discover
If not and you're talking about https://pypi.python.org/pypi/discover then just forget about it - it's a backport for earlier versions of python
4) what to do
either scatter a lot of load_tests hooks across your code as described here https://docs.python.org/2.7/library/doctest.html#unittest-api or code a method to collect all the modules your have in one place and convert them into a DocTestSuite[s] https://docs.python.org/2.7/library/doctest.html#doctest.DocTestSuite
But honestly neither approach makes any sense nowadays as it boils down to:
$ py.test --doctest-modules
or
$ nosetests --with-doctest
Of course coverage and lots of bells & whistles are also supplied by these frameworks and you may keep sticking to unittest.TestCase, and you won't even need to create a coverage_module so I would dig into one of them rather then trying to come up with your own solution
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.
What I want to do is to have several UnitTests written in sikuli, in different files, and then generate a report.
I would want to do something like this:
Project Tests_Thing1.sikuli:
import unittest
class Tests_Thing1(unittest.TestCase):
def setUp(self):
#do some stuff
def tearDown(self):
#do some stuff
def test_Created(self):
#do some sikuli stuff
And there are similar Similar Tests_Thing2 and Tests_Thing3 projects
Project Run_Tests.sikuli:
import unittest
## import siluli projects?
suite = unittest.TestSuite()
suite.addTests(Tests_Thing1)
suite.addTests(Tests_Thing2)
suite.addTests(Tests_Thing3)
suite.run(result)
#generate report from all tests
Is there a way to do this?
I think you can use the standard unittest http://www.jython.org/jythonbook/en/1.0/TestingIntegration.html and import Sikuli into your classes. You need Jython to run it as the following
How to import the sikuli module in python?
https://answers.launchpad.net/sikuli/+question/136170
In my case, I wanted to have the same thing. I wanted to run several tests and create a report.
What I did was created a simple application which ran the selected sikuli scripts one by one on the command line. I collected their output (pass, fail, error messages) in a text file and displayed it again in the app. One can actually write an Html or something.
e.g.
runsikulix.cmd -r TableSelectCells.sikuli >> report.txt
Here you can check more how to do it from the command line.
http://doc.sikuli.org/faq/010-command-line.html
If I have a Python module implemented as a directory (i.e. package) that has both a top level function run and a submodule run, can I count on from example import run to always import the function? Based on my tests that is the case at least with Python 2.6 and Jython 2.5 on Linux, but can I count on this generally? I tried to search information about the import priorities but couldn't find anything.
Background:
I have a pretty large package that people generally run as a tool from the command line but also sometimes use programmatically. I would like to have simple entry points for both usages and consider to implement them like this:
example/__init__.py:
def run(*args):
print args # real application code belongs here
example/run.py:
import sys
from example import run
run(*sys.argv[1:])
The first entry point allows users to access the module from Python like this:
from example import run
run(args)
The latter entry point allows users to execute the module from the command line using both of the approaches below:
python -m example.run args
python path/to/example/run.py args
This both works great and covers everything I need. Before taking this into real use, I would like to know is this a sound approach that I can expect to work with all Python implementations on all operating systems.
I think this should always work; the function definition will shadow the module.
However, this also strikes me as a dirty hack. The clean way to do this would be
# __init__.py
# re-export run.run as run
from .run import run
i.e., a minimal __init__.py, with all the running logic in run.py:
# run.py
def run(*args):
print args # real application code belongs here
if __name__ == "__main__":
run(*sys.argv[1:])
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