Skip to content

Commit 23a002b

Browse files
committed
use subprocess in unittest
1 parent f553144 commit 23a002b

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

python/paddle/fluid/tests/unittests/test_gpu_package_with_no_gpu.py

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
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+
from __future__ import print_function
16+
17+
import os
18+
import sys
19+
import subprocess
20+
import unittest
21+
import paddle
22+
import paddle.fluid as fluid
23+
from paddle.fluid import core
24+
25+
26+
class TestGPUPackagePaddle(unittest.TestCase):
27+
def test_import_paddle(self):
28+
if core.is_compiled_with_cuda():
29+
os.environ['CUDA_VISIBLE_DEVICES'] = ''
30+
test_file = 'test_no_gpu_run_rand.py'
31+
with open(test_file, 'w') as wb:
32+
cmd_test = """
33+
import paddle
34+
x = paddle.rand([3,4])
35+
assert x.place.is_gpu_place() is False, "There is no CUDA device, but Tensor's place is CUDAPlace"
36+
"""
37+
wb.write(cmd_test)
38+
39+
_python = sys.executable
40+
41+
ps_cmd = '{} {}'.format(_python, test_file)
42+
ps_proc = subprocess.Popen(
43+
ps_cmd.strip().split(" "),
44+
stdout=subprocess.PIPE,
45+
stderr=subprocess.PIPE)
46+
stdout, stderr = ps_proc.communicate()
47+
48+
assert 'CPU device will be used by default' in str(
49+
stderr
50+
), "GPU version Paddle is installed. But CPU device can't be used when CUDA device is not set properly"
51+
assert "Error" not in str(
52+
stderr
53+
), "There is no CUDA device, but Tensor's place is CUDAPlace"
54+
55+
56+
if __name__ == '__main__':
57+
unittest.main()

0 commit comments

Comments
 (0)