• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""
16@File   : conftest.py
17@Desc   : common fixtures for pytest
18"""
19
20import pytest
21from _pytest.runner import runtestprotocol
22
23
24def pytest_addoption(parser):
25    """
26    add runmode option to control running testcase
27    """
28    parser.addoption(
29        "--runmode", action="store", default="nosimu",
30        help="simu:simulator backend & nosimu for no backend"
31    )
32    parser.addoption(
33        "--shard", action="store", default="0",
34        help="shard id for parallel pipeline"
35    )
36
37
38@pytest.fixture
39def test_with_simu(request):
40    """
41    run PyNative testcases when compiled with simulator
42    """
43    return request.config.getoption("--runmode") == "simu"
44
45
46@pytest.fixture
47def shard(request):
48    """
49    specify shard id for parallel pipeline testcases
50    """
51    return request.config.getoption("--shard")
52
53
54# https://stackoverflow.com/questions/14121657/how-to-get-test-name-and-test-result-during-run-time-in-pytest
55def pytest_runtest_protocol(item, nextitem):
56    reports = runtestprotocol(item, nextitem=nextitem)
57    for report in reports:
58        if report.when == 'call':
59            print(f"\n{item.name} --- {report.outcome}")
60    return True
61