Controlling the order tests are run in Selenium - python

Here is my code:
class MyTestCase(Base):
def setUp(self):
#some code here
def test_B(self):
#some code here
def test_C(self):
#some code here
def test_A(self):
#some code here
def tearDown(self):
#some code here
if __name__ == "__main__":
unittest.main()
My problem here is that all my tests are executed in alphabetical order, i.e. test_A is first executed, then test_B and then test_C. I want it to execute in the order I have written, i.e. test_B -> test_C -> test_A.
How do I change the order in which the tests are executed?

If your tests need to be in a specific order I think they should be in the same function, but thats just my opinion, check out changing order of unit tests in Python

Related

how to specify test specific setup and teardown in python unittest

I want to create unittest test with two different set up and tearDown methon in same class with two different test.
each test will use its specific setUp and tearDown method in python unittest framework.
could anyone help me.
class processtestCase(unittest.TestCase):
print "start the process test cases "
def setUp1(self):
unittest.TestCase.setUp(self)
def test_test1(self):
"test Functinality"
def tearDown1(self):
unittest.TestCase.tearDown(self)
def setUp2(self):
unittest.TestCase.setUp2(self)
def test_test2(self):
"test Functinality"
def tearDown2(self):
unittest.TestCase.tearDown2(self) '
if __name__ == '__main__':
unittest.main()
In the question, you mention that you have two tests, each with its own setup and teardown. There are at least two ways to go:
You can either embed the setUp and tearDown code into each of the tests:
class FooTest(unittest.TestCase):
def test_0(self):
... # 1st setUp() code
try:
... # 1st test code
except:
... # 1st tearDown() code
raise
def test_1(self):
... # 2nd setUp() code
try:
... # 2nd test code
except:
... # 2nd tearDown() code
raise
Alternatively, you can split the class into two classes:
class FooTest0(unittest.TestCase):
#classmethod
def setUp(cls):
...
#classmethod
def tearDown(cls):
...
def test(self):
...
The first option has fewer classes, is shorter, and more straightforsard. The second option more cleanly decouples setting up the fixture, and cleaning it up, then the test code itself. It also future proofs adding more tests.
You should judge the tradeoffs based on your specific case, and your personal preferences.

Python Define Unit Test Classes Together with Code

I am rapid prototyping so for now I would like to write unit tests together with the code I am testing - rather than putting the tests in a separate file. However, I would only like to build the test an invoke them if we run the code in the containing file as main. As follows:
class MyClass:
def __init(self)__:
# do init stuff here
def myclassFunction():
# do something
if __name__ == '__main__':
import unittest
class TestMyClass(unittest.TestCase):
def test_one(self):
# test myclassFunction()
suite = unittest.TestLoader().loadTestsFromTestCase(TestMyClass)
unittest.TextTestRunner(verbosity=2).run(suite)
This of course doesn't run as unittest is unable to find or make use of the test class, TestMyClass. Is there a way to get this arrangement to work or do I have to pull everything out of the if __name__ == '__main__' scope except the invocation to unittest?
Thanks!
If you move your TestMyClass above of if __name__ == '__main__'
you will get exactly what you want:
Tests will run only when file executed as main
Something like this
import unittest
class MyClass:
def __init(self)__:
# do init stuff here
def myclassFunction():
# do something
class TestMyClass(unittest.TestCase):
def test_one(self):
if __name__ == '__main__':
unittest.main()

Execution order on Python unittest

I need to set an order of execution for my tests, because I need some data verified before the others. Is possible to set an order?
class OneTestCase(unittest.TestCase):
def setUp(self):
# something to do
def test_login (self):
# first test
pass
def test_other (self):
# any order after test_login
def test_othermore (self):
# any order after test_login
if __name__ == '__main__':
unittest.main()
You can do it like this:
class OneTestCase(unittest.TestCase):
#classmethod
def setUpClass(cls):
# something to do
pass
def test_01_login (self):
# first test
pass
def test_02_other (self):
# any order after test_login
def test_03_othermore (self):
# any order after test_login
if __name__ == '__main__':
unittest.main(failfast=True, exit=False)
Tests are sorted alphabetically, so just add numbers to get your desired order. Probably you also want to set failfast = True for the testrunner, so it fails instantly, as soon as the first test fails.
Better do not do it.
Tests should be independent.
To do what you want best would be to put the code into functions that are called by the test.
Like that:
def assert_can_log_in(self):
...
def test_1(self):
self.assert_can_log_in()
...
def test_2(self):
self.assert_can_log_in()
...
Or even to split the test class and put the assertions into the setUp function.
class LoggedInTests(unittest.TestCase):
def setUp(self):
# test for login or not - your decision
def test_1(self):
...
When I split the class I often write more and better tests because the tests are split up and I can see better through all the cases that should be tested.

How to run python unittests repeatedly from a script and collect results

I cannot figure out how to run single unit tests from within a python script and collect the results.
Scenario: I have a battery of tests that check various methods producing various statistical distributions of different objects. The tests sometimes fail, as they should given that I am basically checking for particular kinds of randomness. I would like to run the tests repeatedly from a script or even from the interpreter and collect results for further analysis.
Suppose I have a module myTest.py with:
class myTest(unittest.TestCase):
def setup(self):
...building objects, etc....
def testTest1(self):
..........
def testTest2(self):
..........
Basically I need to:
run the setup method
run testTest1 (say), 100 times
collect the failures
return the failures
The closest I got to was (using code from a similar question):
from unittest import TextTestRunner, TestSuite
runner = TextTestRunner(verbosity = 2)
tests = ['testTest1']
suite = unittest.TestSuite(map(myTest, tests))
runner.run(suite)
But this does not work, because:
runner.run(suite) does not run the setup method
and
I cannot catch the exception it throws when testTest1 fails
you simply need to add the test that you want to run multiple times to the suite.
Here is a complete code example. You can also see this code running in an interactive Python console to prove that it does actually work.
import unittest
import random
class NullWriter(object):
def write(*_, **__):
pass
def flush(*_, **__):
pass
SETUP_COUNTER = 0
class MyTestCase(unittest.TestCase):
def setUp(self):
global SETUP_COUNTER
SETUP_COUNTER += 1
def test_one(self):
self.assertTrue(random.random() > 0.3)
def test_two(self):
# We just want to make sure this isn't run
self.assertTrue(False, "This should not have been run")
def suite():
tests = []
for _ in range(100):
tests.append('test_one')
return unittest.TestSuite(map(MyTestCase, tests))
results = unittest.TextTestRunner(stream=NullWriter()).run(suite())
print dir(results)
print 'setUp was run', SETUP_COUNTER, 'times'

Is test suite deprecated in PyUnit?

Following the example in PyUnit, I came up with the following unittest code that works fine.
import unittest
class Board:
def __init__(self, x, y):
self.x = x; self.y = y;
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class BoardTest(unittest.TestCase):
def setUp(self):
self.b10_10 = Board(10,10)
self.b10_10p = Board(10,10)
self.b10_20 = Board(10,20)
def tearDown(self):
pass
def test1(self):
self.assert_(self.b10_10 == self.b10_10p)
def test2(self):
self.assert_(not (self.b10_10 == self.b10_20))
class BoardTest2(unittest.TestCase):
def setUp(self):
self.b10_10 = Board(10,10)
self.b10_10p = Board(10,10)
self.b10_20 = Board(10,20)
def tearDown(self):
pass
def test1(self):
self.assert_(self.b10_10 == self.b10_10p)
def test2(self):
self.assert_(not (self.b10_10 == self.b10_20))
def suite():
suite1 = unittest.makeSuite(BoardTest)
suite2 = unittest.makeSuite(BoardTest2)
return unittest.TestSuite((suite1, suite2))
if __name__ == "__main__":
unittest.main()
But the thing is that even if I remove the def suite():, the result is the same. In other words, it looks like that the fixture/suite is not useless with PyUnit.
Is this correct?
unittest.TestSuite is not necessary if you want to run all the tests in a single module as unittest.main() will dynamically examine the module it is called from and find all classes that derive from unittest.TestCase
However, the TestSuite class is still handy in a number of scenarios:
You want to build a set of logical groupings of tests. For instance, a suite of unit tests, integration tests, tests for a specific subsystem, etc.
You tests span multiple modules/packages. In this scenario, it is useful to have a single script you can run execute all your tests. This can be accomplished by building up a suite of all your tests. Note that this becomes irrelevant with libraries such as discovery.
In addition to Mark's answer, one more reason to build your own suite() is if you are dynamically building tests.
Also, it took me a while to figure out how to get PyDev to pick up the suite and run it in the graphical test runner. The trick is to put in a method like so:
def load_tests(loader, tests, pattern):
return suite()
Such a method gets picked up the graphical test runner.

Categories

Resources