1 #!/usr/bin/env python 2 # 3 # Copyright 2017, The Android Open Source Project 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 """Unittests for atest.""" 18 19 import unittest 20 import mock 21 22 import atest 23 24 #pylint: disable=protected-access 25 class AtestUnittests(unittest.TestCase): 26 """Unit tests for atest.py""" 27 28 @mock.patch('os.environ.get', return_value=None) 29 def test_missing_environment_variables_uninitialized(self, _): 30 """Test _has_environment_variables when no env vars.""" 31 self.assertTrue(atest._missing_environment_variables()) 32 33 @mock.patch('os.environ.get', return_value='out/testcases/') 34 def test_missing_environment_variables_initialized(self, _): 35 """Test _has_environment_variables when env vars.""" 36 self.assertFalse(atest._missing_environment_variables()) 37 38 def test_parse_args(self): 39 """Test _parse_args parses command line args.""" 40 test_one = 'test_name_one' 41 test_two = 'test_name_two' 42 custom_arg = '--custom_arg' 43 custom_arg_val = 'custom_arg_val' 44 pos_custom_arg = 'pos_custom_arg' 45 46 # Test out test and custom args are properly retrieved. 47 args = [test_one, test_two, '--', custom_arg, custom_arg_val] 48 parsed_args = atest._parse_args(args) 49 self.assertEqual(parsed_args.tests, [test_one, test_two]) 50 self.assertEqual(parsed_args.custom_args, [custom_arg, custom_arg_val]) 51 52 # Test out custom positional args with no test args. 53 args = ['--', pos_custom_arg, custom_arg_val] 54 parsed_args = atest._parse_args(args) 55 self.assertEqual(parsed_args.tests, []) 56 self.assertEqual(parsed_args.custom_args, [pos_custom_arg, 57 custom_arg_val]) 58 59 60 if __name__ == '__main__': 61 unittest.main() 62