xref: /aosp_15_r20/tools/asuite/atest/docs/develop_test_finders.md (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker# Test Finder Developer Guide
2*c2e18aaaSAndroid Build Coastguard Worker
3*c2e18aaaSAndroid Build Coastguard WorkerLearn about test finders and how to create a new test finder class.
4*c2e18aaaSAndroid Build Coastguard Worker
5*c2e18aaaSAndroid Build Coastguard Worker##### Table of Contents
6*c2e18aaaSAndroid Build Coastguard Worker1. [Test Finder Details](#test-finder-details)
7*c2e18aaaSAndroid Build Coastguard Worker2. [Creating a Test Finder](#creating-a-test-finder)
8*c2e18aaaSAndroid Build Coastguard Worker
9*c2e18aaaSAndroid Build Coastguard Worker## <a name="test-finder-details">Test Finder Details</a>
10*c2e18aaaSAndroid Build Coastguard Worker
11*c2e18aaaSAndroid Build Coastguard WorkerA test finder class holds find methods. A find method is given a string (the
12*c2e18aaaSAndroid Build Coastguard Workeruser input) and should try to resolve that string into a ```TestInfo``` object.
13*c2e18aaaSAndroid Build Coastguard WorkerA ```TestInfo``` object holds the test name, test dependencies, test runner, and
14*c2e18aaaSAndroid Build Coastguard Workera data field to hold misc bits like filters and extra args for the test.  The
15*c2e18aaaSAndroid Build Coastguard Workertest finder class can hold multiple find methods. The find methods are grouped
16*c2e18aaaSAndroid Build Coastguard Workertogether in a class so they can share metadata for optimal test finding.
17*c2e18aaaSAndroid Build Coastguard WorkerExamples of metadata would be the ```ModuleInfo``` object or the dirs that hold
18*c2e18aaaSAndroid Build Coastguard Workerthe test configs for the ```TFIntegrationFinder```.
19*c2e18aaaSAndroid Build Coastguard Worker
20*c2e18aaaSAndroid Build Coastguard Worker**When should I create a new test finder class?**
21*c2e18aaaSAndroid Build Coastguard Worker
22*c2e18aaaSAndroid Build Coastguard WorkerIf the metadata used to find a test is unlike existing test finder classes,
23*c2e18aaaSAndroid Build Coastguard Workerthat is the right time to create a new class. Metadata can be anything like
24*c2e18aaaSAndroid Build Coastguard Workerfile name patterns, a special file in a dir to indicate it's a test, etc. The
25*c2e18aaaSAndroid Build Coastguard Workerdefault test finder classes use the module-info.json and specific dir paths
26*c2e18aaaSAndroid Build Coastguard Workermetadata (```ModuleFinder``` and ```TFIntegrationFinder``` respectively).
27*c2e18aaaSAndroid Build Coastguard Worker
28*c2e18aaaSAndroid Build Coastguard Worker## <a name="creating-a-test-finder">Creating a Test Finder</a>
29*c2e18aaaSAndroid Build Coastguard Worker
30*c2e18aaaSAndroid Build Coastguard WorkerFirst thing to choose is where to put the test finder. This will primarily
31*c2e18aaaSAndroid Build Coastguard Workerdepend on if the test finder will be public or private. If public,
32*c2e18aaaSAndroid Build Coastguard Worker```test_finders/``` is the default location.
33*c2e18aaaSAndroid Build Coastguard Worker
34*c2e18aaaSAndroid Build Coastguard Worker> If it will be private, then you can
35*c2e18aaaSAndroid Build Coastguard Worker> follow the same instructions for ```vendorsetup.sh``` in
36*c2e18aaaSAndroid Build Coastguard Worker> [constants override](atest_structure.md#constants-override) where you will
37*c2e18aaaSAndroid Build Coastguard Worker> add the path of where the test finder lives into ```$PYTHONPATH```. Same
38*c2e18aaaSAndroid Build Coastguard Worker> rules apply, rerun ```build/envsetup.sh``` to update ```$PYTHONPATH```.
39*c2e18aaaSAndroid Build Coastguard Worker
40*c2e18aaaSAndroid Build Coastguard WorkerNow define your class and decorate it with the
41*c2e18aaaSAndroid Build Coastguard Worker```test_finder_base.find_method_register``` decorator. This decorator will
42*c2e18aaaSAndroid Build Coastguard Workercreate a list of find methods that ```test_finder_handler``` will use to collect
43*c2e18aaaSAndroid Build Coastguard Workerthe find methods from your test finder class. Take a look at
44*c2e18aaaSAndroid Build Coastguard Worker```test_finders/example_test_finder.py``` as an example.
45*c2e18aaaSAndroid Build Coastguard Worker
46*c2e18aaaSAndroid Build Coastguard WorkerDefine the find methods in your test finder class. These find methods must
47*c2e18aaaSAndroid Build Coastguard Workerreturn a ```TestInfo``` object. Extra bits of info can be stored in the data
48*c2e18aaaSAndroid Build Coastguard Workerfield as a dict.  Check out ```ExampleFinder``` to see how the data field is
49*c2e18aaaSAndroid Build Coastguard Workerused.
50*c2e18aaaSAndroid Build Coastguard Worker
51*c2e18aaaSAndroid Build Coastguard WorkerDecorate each find method with the ```test_finder_base.register``` decorator.
52*c2e18aaaSAndroid Build Coastguard WorkerThis is used by the class decorator to identify the find methods of the class.
53*c2e18aaaSAndroid Build Coastguard Worker
54*c2e18aaaSAndroid Build Coastguard WorkerFinal bit is to add your test finder class to ```test_finder_handler```.
55*c2e18aaaSAndroid Build Coastguard WorkerTry-except import it in the ```_get_test_finders``` method and that should be
56*c2e18aaaSAndroid Build Coastguard Workerit. The find methods will be collected and executed before the default find
57*c2e18aaaSAndroid Build Coastguard Workermethods.
58*c2e18aaaSAndroid Build Coastguard Worker```python
59*c2e18aaaSAndroid Build Coastguard Workertry:
60*c2e18aaaSAndroid Build Coastguard Worker    from test_finders import new_test_finder
61*c2e18aaaSAndroid Build Coastguard Worker    test_finders_list.add(new_test_finder.NewTestFinder)
62*c2e18aaaSAndroid Build Coastguard Workerexcept ImportError:
63*c2e18aaaSAndroid Build Coastguard Worker    pass
64*c2e18aaaSAndroid Build Coastguard Worker```
65