Python unittests, statement before test cases - python

Imagine I have tests like that:
import unittest
class MyTests(unittest.TestCase):
print("Starting")
def test_first(self):
.....
Is the print statement guaranteed to be executed before test_first() and the rest? From what I've seen it does get executed first, but are there any edge cases?

You can use setUp()(docs) and setUpClass()(docs) methods for this. The setUp() method is executed before each individual test while the setUpClass() method is executed before all the tests in this class run.
import unittest
class MyTests(unittest.TestCase):
#classmethod
def setUpClass(cls):
print("Starting all the tests.")
def setUp(self):
print("Starting another test.")
def test_first(self):
...

Related

Call custom function from within Pytest fixture

I would like to write a Pytest fixture in conftest.py that, within itself, calls a function that, by default, does nothing but can be redefined in test modules.
#conftest.py
def within_foo():
pass
#pytest.fixture
def foo():
if some_condition():
within_foo()
something_else()
# test_foo.py
def within_foo():
print('this should be printed when running test_foo')
def test_foo(foo):
pass
Is there a clean way to implement this?
You can accomplish this with inheritance. Create a base class for all tests, than you can decide in which tests you want to override within_foo()
class BaseTest:
#pytest.fixture
def foo(self):
self.within_foo()
def within_foo(self):
pass
#pytest.mark.testing
class TestSomething(BaseTest):
def within_foo(self):
print('this should be printed when running test_foo')
def test_foo(self, foo):
pass
#pytest.mark.testing
class TestSomethingElse(BaseTest):
def test_foo_again(self, foo):
print('test_foo_again')
Outout
========================== 2 passed in 0.08 seconds ===========================
this should be printed when running test_foo
.test_foo
.test_foo_again

Python nose setup/teardown class fixture method not executed for test generators

In a hobby project I intend to use nose for testing, I want to put all tests for specific classes into classes since these tests share setup and other functionality. But I can't seem to get nose to execute the setup methods inside the classes.
Here is an example class that is tested:
class mwe():
def __init__(self):
self.example = ""
def setExample(self, ex):
self.example = ex
The tests work, when I don't use classes:
from nose.tools import ok_
import mwe
exampleList = []
def setUp():
print("setup")
exampleList.append("1")
exampleList.append("2")
exampleList.append("3")
def test_Example():
print("test")
for ex in exampleList:
t = mwe.mwe()
t.setExample(ex)
yield check, t, ex
def check(e, ex):
ok_(e.example == ex)
The output is as expected:
setup
test
...
----------------------------------------------------------------------
Ran 3 tests in 0.004s
OK
When a test class is used, the setup method is not executed and therefore no tests are executed.
from nose.tools import ok_
import mwe
class TestexampleClass(object):
def __init__(self):
print("__init__")
self.exampleList = []
def setup(self):
print("setup class")
self.exampleList.append("1")
self.exampleList.append("2")
self.exampleList.append("3")
def test_ExampleClass(self):
print("test class")
for ex in self.exampleList:
t = mwe.mwe()
t.setExample(ex)
yield self.check, t, ex
def check(self, we, ex):
print("check class")
ok_(we.example == ex)
I'm fairly new to python and a newbie with nose, my question is, why is setup not executed? Where is the error in my code?
__init__
test class
----------------------------------------------------------------------
Ran 0 tests in 0.002s
OK
I will be glad for any feedback.
When I use the code from this Question on SO the setup method is executed, as I would expect it.
SOLUTION: After a lot of desperation I found the following:
Nose executes the the class level setup method before the execution of the yielded function, not when the test_* methods are called, as I expected and as is the case for other test_* methods. This obviously goes against the nose documentation:
Setup and teardown functions may be used with test generators. However, please note that setup and teardown attributes attached to the generator function will execute only once. To execute fixtures for each yielded test, attach the setup and teardown attributes to the function that is yielded, or yield a callable object instance with setup and teardown attributes.
Looking at the bug reports, I found the bug report on github.
A possible workaround is to use class level fixtures:
#classmethod
def setup_class(cls):
#do stuff
pass
Your test class needs to extend TestCase and the setup method needs to be called setUp
from unittest import TestCase
class TestUtils(TestCase):
def setUp(self):
self.x = 1
def test_something(self):
self.assertEqual(1, self.x)
which outputs
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK

Pyunit framework extending the classes of unittest

In Pyunit framework, I have question as below:
import unittest
class xyz(object):
def test_fuc(self):
print "test_fun"
pass
class abc(unittest.Testcase, xyz):
def setUp(self):
print "setUp"
def tearDown(self):
print "tearDown"
def test_one(self):
print "test_one"
pass
def test_two(self):
print "test_two"
pass
Output:
test_one
test_two
test_fun
but I need output like below:
test_one
test_fun
test_two
test_fun
Please suggest, How do I do it?
Unit tests (ie. unittest.TestCase methods) are supposed to be independant, and thus there is no point of running them twice.
If you want to do that, you probably should design your tests another way.

Why mocking is still effective after existing a code in nosetests multiple tests?

I have the following code test_A.py which mocks MyClass.mymethod:
from unittest import main
from mocker import Mocker, MockerTestCase
Class test_A(MockerTestCase):
def setUp(self):
self.m=Mock()
MyClass.mymethod = self.m.mock()
self.m.result(None)
self.m.count(0,None)
self.m.replay()
def test_me(self):
#Do something about MyClass.method
def tearDown(self):
self.m.restore()
self.m.verify()
I also have another code test_B.py which DOES NOT mock MyClass.mymethod:
Class test_B(MockerTestCase):
def setUp(self):
pass
def test_me(self):
#Do something about MyClass.method
def tearDown(self):
pass
However, when I do "nosetests test_A.py test_B.py", it looks like after testing test_A.py and entering test_B.py, MyClass.mymethod is still mocked up. Not sure why and how to get around it. Thanks!
The line:
MyClass.mymethod = self.m.mock()
really does replace MyClass.mymethod() with a new object. All subsequent references to MyClass.mymethod will be to the mock object, even if those references are in a different class.
What you want is a way to replace MyClass.mymethod() that only works in class test_A. The simplest way to achieve this would be to restore the original mymethod in your tearDown method:
Class test_A():
def setUp(self):
self.originalMyMethod = MyClass.mymethod
self.m=Mock()
MyClass.mymethod = self.m.mock()
self.m.result(None)
self.m.count(0,None)
self.m.replay()
def test_me(self):
# Do something about MyClass.mymethod
def tearDown(self):
self.m.restore()
self.m.verify()
MyClass.mymethod = self.originalMyMethod

Unittest setUp/tearDown for several tests

Is there a function that is fired at the beginning/end of a scenario of tests? The functions setUp and tearDown are fired before/after every single test.
I typically would like to have this:
class TestSequenceFunctions(unittest.TestCase):
def setUpScenario(self):
start() #launched at the beginning, once
def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)
def test_sample(self):
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
def tearDownScenario(self):
end() #launched at the end, once
For now, these setUp and tearDown are unit tests and spread in all my scenarios (containing many tests), one is the first test, the other is the last test.
As of 2.7 (per the documentation) you get setUpClass and tearDownClass which execute before and after the tests in a given class are run, respectively. Alternatively, if you have a group of them in one file, you can use setUpModule and tearDownModule (documentation).
Otherwise your best bet is probably going to be to create your own derived TestSuite and override run(). All other calls would be handled by the parent, and run would call your setup and teardown code around a call up to the parent's run method.
I have the same scenario, for me setUpClass and tearDownClass methods works perfectly
import unittest
class Test(unittest.TestCase):
#classmethod
def setUpClass(cls):
cls._connection = createExpensiveConnectionObject()
#classmethod
def tearDownClass(cls):
cls._connection.destroy()
Here is an example: 3 test methods access a shared resource, which is created once, not per test.
import unittest
import random
class TestSimulateLogistics(unittest.TestCase):
shared_resource = None
#classmethod
def setUpClass(cls):
cls.shared_resource = random.randint(1, 100)
#classmethod
def tearDownClass(cls):
cls.shared_resource = None
def test_1(self):
print('test 1:', self.shared_resource)
def test_2(self):
print('test 2:', self.shared_resource)
def test_3(self):
print('test 3:', self.shared_resource)
For python 2.5, and when working with pydev, it's a bit hard. It appears that pydev doesn't use the test suite, but finds all individual test cases and runs them all separately.
My solution for this was using a class variable like this:
class TestCase(unittest.TestCase):
runCount = 0
def setUpClass(self):
pass # overridden in actual testcases
def run(self, result=None):
if type(self).runCount == 0:
self.setUpClass()
super(TestCase, self).run(result)
type(self).runCount += 1
With this trick, when you inherit from this TestCase (instead of from the original unittest.TestCase), you'll also inherit the runCount of 0. Then in the run method, the runCount of the child testcase is checked and incremented. This leaves the runCount variable for this class at 0.
This means the setUpClass will only be ran once per class and not once per instance.
I don't have a tearDownClass method yet, but I guess something could be made with using that counter.
import unittest
class Test(unittest.TestCase):
#classmethod
def setUpClass(cls):
cls.shared_data = "dddd"
#classmethod
def tearDownClass(cls):
cls.shared_data.destroy()
def test_one(self):
print("Test one")
def test_two(self):
print("Test 2")
For more visit Python
unit test document

Categories

Resources