Convenience function to define pytest fixtures. This should be used as a decorator to mark functions that set up state before a function. The return value of that fixture function is then accessible by test functions as that accept it as a parameter. Fixture functions can acce
(func=None, *, cache_return_value=False)
| 866 | |
| 867 | |
| 868 | def fixture(func=None, *, cache_return_value=False): |
| 869 | """Convenience function to define pytest fixtures. |
| 870 | |
| 871 | This should be used as a decorator to mark functions that set up |
| 872 | state before a function. The return value of that fixture |
| 873 | function is then accessible by test functions as that accept it as |
| 874 | a parameter. |
| 875 | |
| 876 | Fixture functions can accept parameters defined with |
| 877 | :py:func:`tvm.testing.parameter`. |
| 878 | |
| 879 | By default, the setup will be performed once for each unit test |
| 880 | that uses a fixture, to ensure that unit tests are independent. |
| 881 | If the setup is expensive to perform, then the |
| 882 | cache_return_value=True argument can be passed to cache the setup. |
| 883 | The fixture function will be run only once (or once per parameter, |
| 884 | if used with tvm.testing.parameter), and the same return value |
| 885 | will be passed to all tests that use it. If the environment |
| 886 | variable TVM_TEST_DISABLE_CACHE is set to a non-zero value, it |
| 887 | will disable this feature and no caching will be performed. |
| 888 | |
| 889 | Example |
| 890 | ------- |
| 891 | >>> @tvm.testing.fixture |
| 892 | >>> def cheap_setup(): |
| 893 | >>> return 5 # Setup code here. |
| 894 | >>> |
| 895 | >>> def test_feature_x(target, dev, cheap_setup) |
| 896 | >>> assert(cheap_setup == 5) # Run test here |
| 897 | |
| 898 | Or |
| 899 | |
| 900 | >>> size = tvm.testing.parameter(1, 10, 100) |
| 901 | >>> |
| 902 | >>> @tvm.testing.fixture |
| 903 | >>> def cheap_setup(size): |
| 904 | >>> return 5*size # Setup code here, based on size. |
| 905 | >>> |
| 906 | >>> def test_feature_x(cheap_setup): |
| 907 | >>> assert(cheap_setup in [5, 50, 500]) |
| 908 | |
| 909 | Or |
| 910 | |
| 911 | >>> @tvm.testing.fixture(cache_return_value=True) |
| 912 | >>> def expensive_setup(): |
| 913 | >>> time.sleep(10) # Setup code here |
| 914 | >>> return 5 |
| 915 | >>> |
| 916 | >>> def test_feature_x(target, dev, expensive_setup): |
| 917 | >>> assert(expensive_setup == 5) |
| 918 | |
| 919 | """ |
| 920 | |
| 921 | force_disable_cache = bool(int(os.environ.get("TVM_TEST_DISABLE_CACHE", "0"))) |
| 922 | cache_return_value = cache_return_value and not force_disable_cache |
| 923 | |
| 924 | # Deliberately at function scope, so that caching can track how |
| 925 | # many times the fixture has been used. If used, the cache gets |