Reputation: 24778
I have this:
import unittest
import sys, os
sys.path.append(os.path.dirname(sys.argv[0]))
class TestStringMethods(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.g = "def"
print cls
def test_upper(self):
self.assertEqual('DeF'.lower(), TestStringMethods.g)
if __name__ == '__main__':
unittest.main()
This
python test.py
gives:
python screen_test.py
<class '__main__.TestStringMethods'>
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
But, this:
monkeyrunner "%CD%\test.py"
gives:
E
======================================================================
ERROR: test_upper (__main__.TestStringMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\abc\def\ghi\jkl\test.py", line 29, in test_upper
self.assertEqual('DeF'.lower(), TestStringMethods.g)
AttributeError: type object 'TestStringMethods' has no attribute 'g'
----------------------------------------------------------------------
Ran 1 test in 0.024s
FAILED (errors=1)
Why is the same test failing when run with monkeyrunner ?
Also where is that lone E
coming from?
Upvotes: 0
Views: 167
Reputation: 69396
As you may have discovered already, this is because monkeyrunner
is not running the setUpClass
method.
You can use AndroidViewClient/culebra as a drop-in replacement for monkeyrunner
with the advantage that it runs with python 2.x, so your tests will be correctly initialized.
Furthermore, culebra -U
could generate tests automatically, which you can then customize.
This is a snippet from a generated test (with some lines removed for clarity):
#! /usr/bin/env python
# ...
import unittest
from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase
TAG = 'CULEBRA'
class CulebraTests(CulebraTestCase):
@classmethod
def setUpClass(cls):
# ...
cls.sleep = 5
def setUp(self):
super(CulebraTests, self).setUp()
def tearDown(self):
super(CulebraTests, self).tearDown()
def preconditions(self):
if not super(CulebraTests, self).preconditions():
return False
return True
def testSomething(self):
if not self.preconditions():
self.fail('Preconditions failed')
_s = CulebraTests.sleep
_v = CulebraTests.verbose
## your test code here ##
if __name__ == '__main__':
CulebraTests.main()
CulebraTestCase
provides the heavy lifting, connecting the test with adb
and the available devices, processing the command line options, etc.
Upvotes: 1