1 // Copyright 2022 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_CHECK_IS_TEST_H_ 6 #define BASE_CHECK_IS_TEST_H_ 7 8 #include "base/base_export.h" 9 10 // Code paths taken in tests are sometimes different from those taken in 11 // production. This might be because the respective tests do not initialize some 12 // objects that would be required for the "normal" code path. 13 // 14 // Ideally, such code constructs should be avoided, so that tests really test 15 // the production code and not something different. 16 // 17 // However, there already are hundreds of test-only paths in production code 18 // Cleaning up all these cases retroactively and completely avoiding such cases 19 // in the future seems unrealistic. 20 // 21 // Thus, it is useful to prevent the test code-only paths to be taken in 22 // production scenarios. 23 // 24 // `CHECK_IS_TEST` can be used to assert that a test-only path is actually taken 25 // only in tests. For instance: 26 // 27 // // This only happens in unit tests: 28 // if (!url_loader_factory) 29 // { 30 // // Assert that this code path is really only taken in tests. 31 // CHECK_IS_TEST(); 32 // return; 33 // } 34 // 35 // `CHECK_IS_TEST` is thread safe. 36 37 #define CHECK_IS_TEST() base::internal::check_is_test_impl() 38 39 namespace base::internal { 40 BASE_EXPORT void check_is_test_impl(); 41 } // namespace base::internal 42 43 #endif // BASE_CHECK_IS_TEST_H_ 44