15a5b4906SAdrian PrantlThis README file describes the files and directories related       -*- rst -*-
25a5b4906SAdrian Prantlto the Python test suite under the current 'test' directory.
3c432c8f8SZachary Turner
45a5b4906SAdrian Prantl- dotest.py
5c432c8f8SZachary Turner
6c432c8f8SZachary Turner  Provides the test driver for the test suite.  To invoke it, cd to the 'test'
7c432c8f8SZachary Turner  directory and issue the './dotest.py' command or './dotest.py -v' for more
8c432c8f8SZachary Turner  verbose output.  '.dotest.py -h' prints out the help messge.
9c432c8f8SZachary Turner
10c432c8f8SZachary Turner  A specific naming pattern is followed by the .py script under the 'test'
11c432c8f8SZachary Turner  directory in order to be recognized by 'dotest.py' test driver as a module
12c432c8f8SZachary Turner  which implements a test case, namely, Test*.py.
13c432c8f8SZachary Turner
14c432c8f8SZachary Turner  Some example usages:
15c432c8f8SZachary Turner
16c432c8f8SZachary Turner  1. ./dotest.py -v . 2> ~/Developer/Log/lldbtest.log0
17c432c8f8SZachary Turner     This runs the test suite and directs the run log to a file.
18c432c8f8SZachary Turner
19c432c8f8SZachary Turner  2. LLDB_LOG=/tmp/lldb.log GDB_REMOTE_LOG=/tmp/gdb-remote.log ./dotest.py -v . 2> ~/Developer/Log/lldbtest.log
20c432c8f8SZachary Turner     This runs the test suite, with logging turned on for the lldb as well as
21c432c8f8SZachary Turner     the process.gdb-remote channels and directs the run log to a file.
22c432c8f8SZachary Turner
235a5b4906SAdrian Prantl- lldbtest.py
24c432c8f8SZachary Turner
25c432c8f8SZachary Turner  Provides an abstract base class of lldb test case named 'TestBase', which in
26c432c8f8SZachary Turner  turn inherits from Python's unittest.TestCase.  The concrete subclass can
27c432c8f8SZachary Turner  override lldbtest.TestBase in order to inherit the common behavior for
28c432c8f8SZachary Turner  unittest.TestCase.setUp/tearDown implemented in this file.
29c432c8f8SZachary Turner
30c432c8f8SZachary Turner  To provide a test case, the concrete subclass provides methods whose names
31c432c8f8SZachary Turner  start with the letters test.  For more details about the Python's unittest
32c432c8f8SZachary Turner  framework, go to http://docs.python.org/library/unittest.html.
33c432c8f8SZachary Turner
34c432c8f8SZachary Turner  ./command_source/TestCommandSource.py provides a simple example of test case
35c432c8f8SZachary Turner  which overrides lldbtest.TestBase to exercise the lldb's 'command source'
36*4cc8f2a0SDave Lee  command.
37c432c8f8SZachary Turner
38c432c8f8SZachary Turner  The doc string provides more details about the setup required for running a
39c432c8f8SZachary Turner  test case on its own.  To run the whole test suite, 'dotest.py' is all you
40c432c8f8SZachary Turner  need to do.
41c432c8f8SZachary Turner
425a5b4906SAdrian Prantl- subdirectories of 'test'
43c432c8f8SZachary Turner
44c432c8f8SZachary Turner  Most of them predate the introduction of the python test suite and contain
45c432c8f8SZachary Turner  example C/C++/ObjC source files which get compiled into executables which are
46c432c8f8SZachary Turner  to be exercised by the debugger.
47c432c8f8SZachary Turner
48c432c8f8SZachary Turner  For such subdirectory which has an associated Test*.py file, it was added as
49c432c8f8SZachary Turner  part of the Python-based test suite to test lldb functionality.
50c432c8f8SZachary Turner
51c432c8f8SZachary Turner  Some of the subdirectories, for example, the 'help' subdirectory, do not have
52c432c8f8SZachary Turner  C/C++/ObjC source files; they were created to house the Python test case which
53c432c8f8SZachary Turner  does not involve lldb reading in an executable file at all.
54c432c8f8SZachary Turner
5596b2530fSJim Ingham  The sample_test directory contains examples of both a full and an "inline"
5696b2530fSJim Ingham  testcase that run a process to a breakpoint and check a local variable.  These
5796b2530fSJim Ingham  are convenient starting points for adding new tests.
5896b2530fSJim Ingham
595a5b4906SAdrian Prantl- make directory
60c432c8f8SZachary Turner
61c432c8f8SZachary Turner  Contains Makefile.rules, which can be utilized by test cases to write Makefile
62c432c8f8SZachary Turner  based rules to build binaries for the inferiors.
63c432c8f8SZachary Turner
64c432c8f8SZachary Turner  By default, the built executable name is a.out, which can be overwritten by
65c432c8f8SZachary Turner  specifying your EXE make variable, via the Makefile under the specific test
66c432c8f8SZachary Turner  directory or via supplying a Python dictionary to the build method in your
67c432c8f8SZachary Turner  Python test script.  An example of the latter can be found in
68c432c8f8SZachary Turner  test/lang/objc/radar-9691614/TestObjCMethodReturningBOOL.py, where:
69c432c8f8SZachary Turner
708bac18beSPavel Labath    def test_method_ret_BOOL(self):
71c432c8f8SZachary Turner        """Test that objective-c method returning BOOL works correctly."""
72c432c8f8SZachary Turner        d = {'EXE': self.exe_name}
738bac18beSPavel Labath        self.build(dictionary=d)
74c432c8f8SZachary Turner        self.setTearDownCleanup(dictionary=d)
758bac18beSPavel Labath        ...
76c432c8f8SZachary Turner
77c432c8f8SZachary Turner    def setUp(self):
78c432c8f8SZachary Turner        # Call super's setUp().
79c432c8f8SZachary Turner        TestBase.setUp(self)
80c432c8f8SZachary Turner        # We'll use the test method name as the exe_name.
81c432c8f8SZachary Turner        self.exe_name = self.testMethodName
82c432c8f8SZachary Turner        # Find the line number to break inside main().
83c432c8f8SZachary Turner        self.main_source = "main.m"
84c432c8f8SZachary Turner        self.line = line_number(self.main_source, '// Set breakpoint here.')
85c432c8f8SZachary Turner
86c432c8f8SZachary Turner  The exe names for the two test methods are equal to the test method names and
87c432c8f8SZachary Turner  are therefore guaranteed different.
88c432c8f8SZachary Turner
895a5b4906SAdrian Prantl- plugins directory
90c432c8f8SZachary Turner
91c432c8f8SZachary Turner  Contains platform specific plugin to build binaries with dsym/dwarf debugging
92c432c8f8SZachary Turner  info.  Other platform specific functionalities may be added in the future.
93c432c8f8SZachary Turner
945a5b4906SAdrian Prantl- unittest2 directory
95c432c8f8SZachary Turner
96c432c8f8SZachary Turner  Many new features were added to unittest in Python 2.7, including test
97c432c8f8SZachary Turner  discovery. unittest2 allows you to use these features with earlier versions of
98c432c8f8SZachary Turner  Python.
99c432c8f8SZachary Turner
100c432c8f8SZachary Turner  It currently has unittest2 0.5.1 from http://pypi.python.org/pypi/unittest2.
101c432c8f8SZachary Turner  Version 0.5.1 of unittest2 has feature parity with unittest in Python 2.7
102c432c8f8SZachary Turner  final. If you want to ensure that your tests run identically under unittest2
103c432c8f8SZachary Turner  and unittest in Python 2.7 you should use unittest2 0.5.1.
104c432c8f8SZachary Turner
105c432c8f8SZachary Turner  Later versions of unittest2 include changes in unittest made in Python 3.2 and
106c432c8f8SZachary Turner  onwards after the release of Python 2.7.
107c432c8f8SZachary Turner
1085a5b4906SAdrian Prantl- dotest.pl
109c432c8f8SZachary Turner
110c432c8f8SZachary Turner  In case you wonder, there is also a 'dotest.pl' perl script file.  It was
111c432c8f8SZachary Turner  created to visit each Python test case under the specified directory and
112c432c8f8SZachary Turner  invoke Python's builtin unittest.main() on each test case.
113c432c8f8SZachary Turner
114c432c8f8SZachary Turner  It does not take advantage of the test runner and test suite functionality
115c432c8f8SZachary Turner  provided by Python's unitest framework.  Its existence is because we want a
116c432c8f8SZachary Turner  different way of running the whole test suite.  As lldb and the Python test
117c432c8f8SZachary Turner  suite become more reliable, we don't expect to be using 'dotest.pl' anymore.
118c432c8f8SZachary Turner
119c432c8f8SZachary Turner  Note: dotest.pl has been moved to the attic directory.
120c432c8f8SZachary Turner
1215a5b4906SAdrian Prantl- Profiling dotest.py runs
122c432c8f8SZachary Turner
123c432c8f8SZachary Turner  I used the following command line thingy to do the profiling on a SnowLeopard
124c432c8f8SZachary Turner  machine:
125c432c8f8SZachary Turner
126c432c8f8SZachary Turner    $ DOTEST_PROFILE=YES DOTEST_SCRIPT_DIR=/Volumes/data/lldb/svn/trunk/test /System/Library/Frameworks/Python.framework/Versions/Current/lib/python2.6/cProfile.py -o my.profile ./dotest.py -v -w 2> ~/Developer/Log/lldbtest.log
127c432c8f8SZachary Turner
128c432c8f8SZachary Turner  After that, I used the pstats.py module to browse the statistics:
129c432c8f8SZachary Turner
130c432c8f8SZachary Turner    $ python /System/Library/Frameworks/Python.framework/Versions/Current/lib/python2.6/pstats.py my.profile
131c432c8f8SZachary Turner
1325a5b4906SAdrian Prantl- Writing test cases:
133c432c8f8SZachary Turner
1345a5b4906SAdrian Prantl  We strongly prefer writing test cases using the SB API's rather than
1355a5b4906SAdrian Prantl  the runCmd & expect.  Unless you are actually testing some feature
1365a5b4906SAdrian Prantl  of the command line, please don't write command based tests.  For
1375a5b4906SAdrian Prantl  historical reasons there are plenty of examples of tests in the test
1385a5b4906SAdrian Prantl  suite that use runCmd where they shouldn't, but don't copy them,
1395a5b4906SAdrian Prantl  copy the plenty that do use the SB API's instead.
140c432c8f8SZachary Turner
1415a5b4906SAdrian Prantl  The reason for this is that our policy is that we will maintain
1425a5b4906SAdrian Prantl  compatibility with the SB API's.  But we don't make any similar
1435a5b4906SAdrian Prantl  guarantee about the details of command result format.  If your test
1445a5b4906SAdrian Prantl  is using the command line, it is going to have to check against the
1455a5b4906SAdrian Prantl  command result text, and you either end up writing your check
1465a5b4906SAdrian Prantl  pattern by checking as little as possible so you won't be exposed to
1475a5b4906SAdrian Prantl  random changes in the text; in which case you can end up missing
1485a5b4906SAdrian Prantl  some failure, or you test too much and it means irrelevant changes
1495a5b4906SAdrian Prantl  break your tests.
150c432c8f8SZachary Turner
1515a5b4906SAdrian Prantl  However, if you use the Python API's it is possible to check all the
1525a5b4906SAdrian Prantl  results you want to check in a very explicit way, which makes the
1535a5b4906SAdrian Prantl  tests much more robust.
154c432c8f8SZachary Turner
1555a5b4906SAdrian Prantl  Even if you are testing that a command-line command does some
1565a5b4906SAdrian Prantl  specific thing, it is still better in general to use the SB API's to
1575a5b4906SAdrian Prantl  drive to the point where you want to run the test, then use
1585a5b4906SAdrian Prantl  SBInterpreter::HandleCommand to run the command.  You get the full
1595a5b4906SAdrian Prantl  result text from the command in the command return object, and all
1605a5b4906SAdrian Prantl  the part where you are driving the debugger to the point you want to
1615a5b4906SAdrian Prantl  test will be more robust.
162d0f89cd7SPavel Labath
1635a5b4906SAdrian Prantl  The sample_test directory contains a standard and an "inline" test
1645a5b4906SAdrian Prantl  that are good starting points for writing a new test.
16596b2530fSJim Ingham
1665a5b4906SAdrian Prantl- Attaching in test cases:
167d0f89cd7SPavel Labath
1685a5b4906SAdrian Prantl  If you need to attach to inferiors in your tests, you must make sure
1695a5b4906SAdrian Prantl  the inferior calls lldb_enable_attach(), before the debugger
1705a5b4906SAdrian Prantl  attempts to attach. This function performs any platform-specific
1715a5b4906SAdrian Prantl  processing needed to enable attaching to this process (e.g., on
1725a5b4906SAdrian Prantl  Linux, we execute prctl(PR_SET_TRACER) syscall to disable
1735a5b4906SAdrian Prantl  protections present in some Linux systems).
174