Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

# 

# Licensed to the Apache Software Foundation (ASF) under one or more 

# contributor license agreements. See the NOTICE file distributed with 

# this work for additional information regarding copyright ownership. 

# The ASF licenses this file to You under the Apache License, Version 2.0 

# (the "License"); you may not use this file except in compliance with 

# the License. You may obtain a copy of the License at 

# 

# http://www.apache.org/licenses/LICENSE-2.0 

# 

# Unless required by applicable law or agreed to in writing, software 

# distributed under the License is distributed on an "AS IS" BASIS, 

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

# See the License for the specific language governing permissions and 

# limitations under the License. 

# 

 

import os 

import shutil 

import tempfile 

import time 

 

from pyspark.sql import Row 

from pyspark.sql.functions import lit 

from pyspark.sql.types import StructType, StructField, IntegerType, StringType 

from pyspark.testing.sqlutils import ReusedSQLTestCase 

 

 

class StreamingTests(ReusedSQLTestCase): 

 

def test_stream_trigger(self): 

df = self.spark.readStream.format('text').load('python/test_support/sql/streaming') 

 

# Should take at least one arg 

try: 

df.writeStream.trigger() 

except ValueError: 

pass 

 

# Should not take multiple args 

try: 

df.writeStream.trigger(once=True, processingTime='5 seconds') 

except ValueError: 

pass 

 

# Should not take multiple args 

try: 

df.writeStream.trigger(processingTime='5 seconds', continuous='1 second') 

except ValueError: 

pass 

 

# Should take only keyword args 

try: 

df.writeStream.trigger('5 seconds') 

self.fail("Should have thrown an exception") 

except TypeError: 

pass 

 

def test_stream_read_options(self): 

schema = StructType([StructField("data", StringType(), False)]) 

df = self.spark.readStream\ 

.format('text')\ 

.option('path', 'python/test_support/sql/streaming')\ 

.schema(schema)\ 

.load() 

self.assertTrue(df.isStreaming) 

self.assertEqual(df.schema.simpleString(), "struct<data:string>") 

 

def test_stream_read_options_overwrite(self): 

bad_schema = StructType([StructField("test", IntegerType(), False)]) 

schema = StructType([StructField("data", StringType(), False)]) 

# SPARK-32516 disables the overwrite behavior by default. 

with self.sql_conf({"spark.sql.legacy.pathOptionBehavior.enabled": True}): 

df = self.spark.readStream.format('csv')\ 

.option('path', 'python/test_support/sql/fake')\ 

.schema(bad_schema)\ 

.load(path='python/test_support/sql/streaming', schema=schema, format='text') 

self.assertTrue(df.isStreaming) 

self.assertEqual(df.schema.simpleString(), "struct<data:string>") 

 

def test_stream_save_options(self): 

df = self.spark.readStream.format('text').load('python/test_support/sql/streaming') \ 

.withColumn('id', lit(1)) 

84 ↛ 85line 84 didn't jump to line 85, because the loop on line 84 never started for q in self.spark._wrapped.streams.active: 

q.stop() 

tmpPath = tempfile.mkdtemp() 

shutil.rmtree(tmpPath) 

self.assertTrue(df.isStreaming) 

out = os.path.join(tmpPath, 'out') 

chk = os.path.join(tmpPath, 'chk') 

q = df.writeStream.option('checkpointLocation', chk).queryName('this_query') \ 

.format('parquet').partitionBy('id').outputMode('append').option('path', out).start() 

try: 

self.assertEqual(q.name, 'this_query') 

self.assertTrue(q.isActive) 

q.processAllAvailable() 

output_files = [] 

for _, _, files in os.walk(out): 

output_files.extend([f for f in files if not f.startswith('.')]) 

self.assertTrue(len(output_files) > 0) 

self.assertTrue(len(os.listdir(chk)) > 0) 

finally: 

q.stop() 

shutil.rmtree(tmpPath) 

 

def test_stream_save_options_overwrite(self): 

df = self.spark.readStream.format('text').load('python/test_support/sql/streaming') 

108 ↛ 109line 108 didn't jump to line 109, because the loop on line 108 never started for q in self.spark._wrapped.streams.active: 

q.stop() 

tmpPath = tempfile.mkdtemp() 

shutil.rmtree(tmpPath) 

self.assertTrue(df.isStreaming) 

out = os.path.join(tmpPath, 'out') 

chk = os.path.join(tmpPath, 'chk') 

fake1 = os.path.join(tmpPath, 'fake1') 

fake2 = os.path.join(tmpPath, 'fake2') 

# SPARK-32516 disables the overwrite behavior by default. 

with self.sql_conf({"spark.sql.legacy.pathOptionBehavior.enabled": True}): 

q = df.writeStream.option('checkpointLocation', fake1)\ 

.format('memory').option('path', fake2) \ 

.queryName('fake_query').outputMode('append') \ 

.start(path=out, format='parquet', queryName='this_query', checkpointLocation=chk) 

 

try: 

self.assertEqual(q.name, 'this_query') 

self.assertTrue(q.isActive) 

q.processAllAvailable() 

output_files = [] 

for _, _, files in os.walk(out): 

output_files.extend([f for f in files if not f.startswith('.')]) 

self.assertTrue(len(output_files) > 0) 

self.assertTrue(len(os.listdir(chk)) > 0) 

self.assertFalse(os.path.isdir(fake1)) # should not have been created 

self.assertFalse(os.path.isdir(fake2)) # should not have been created 

finally: 

q.stop() 

shutil.rmtree(tmpPath) 

 

def test_stream_status_and_progress(self): 

df = self.spark.readStream.format('text').load('python/test_support/sql/streaming') 

141 ↛ 142line 141 didn't jump to line 142, because the loop on line 141 never started for q in self.spark._wrapped.streams.active: 

q.stop() 

tmpPath = tempfile.mkdtemp() 

shutil.rmtree(tmpPath) 

self.assertTrue(df.isStreaming) 

out = os.path.join(tmpPath, 'out') 

chk = os.path.join(tmpPath, 'chk') 

 

def func(x): 

time.sleep(1) 

return x 

 

from pyspark.sql.functions import col, udf 

sleep_udf = udf(func) 

 

# Use "sleep_udf" to delay the progress update so that we can test `lastProgress` when there 

# were no updates. 

q = df.select(sleep_udf(col("value")).alias('value')).writeStream \ 

.start(path=out, format='parquet', queryName='this_query', checkpointLocation=chk) 

try: 

# "lastProgress" will return None in most cases. However, as it may be flaky when 

# Jenkins is very slow, we don't assert it. If there is something wrong, "lastProgress" 

# may throw error with a high chance and make this test flaky, so we should still be 

# able to detect broken codes. 

q.lastProgress 

 

q.processAllAvailable() 

lastProgress = q.lastProgress 

recentProgress = q.recentProgress 

status = q.status 

self.assertEqual(lastProgress['name'], q.name) 

self.assertEqual(lastProgress['id'], q.id) 

173 ↛ exitline 173 didn't finish the generator expression on line 173 self.assertTrue(any(p == lastProgress for p in recentProgress)) 

self.assertTrue( 

"message" in status and 

"isDataAvailable" in status and 

"isTriggerActive" in status) 

finally: 

q.stop() 

shutil.rmtree(tmpPath) 

 

def test_stream_await_termination(self): 

df = self.spark.readStream.format('text').load('python/test_support/sql/streaming') 

184 ↛ 185line 184 didn't jump to line 185, because the loop on line 184 never started for q in self.spark._wrapped.streams.active: 

q.stop() 

tmpPath = tempfile.mkdtemp() 

shutil.rmtree(tmpPath) 

self.assertTrue(df.isStreaming) 

out = os.path.join(tmpPath, 'out') 

chk = os.path.join(tmpPath, 'chk') 

q = df.writeStream\ 

.start(path=out, format='parquet', queryName='this_query', checkpointLocation=chk) 

try: 

self.assertTrue(q.isActive) 

try: 

q.awaitTermination("hello") 

self.fail("Expected a value exception") 

except ValueError: 

pass 

now = time.time() 

# test should take at least 2 seconds 

res = q.awaitTermination(2.6) 

duration = time.time() - now 

self.assertTrue(duration >= 2) 

self.assertFalse(res) 

finally: 

q.processAllAvailable() 

q.stop() 

shutil.rmtree(tmpPath) 

 

def test_stream_exception(self): 

sdf = self.spark.readStream.format('text').load('python/test_support/sql/streaming') 

sq = sdf.writeStream.format('memory').queryName('query_explain').start() 

try: 

sq.processAllAvailable() 

self.assertEqual(sq.exception(), None) 

finally: 

sq.stop() 

 

from pyspark.sql.functions import col, udf 

from pyspark.sql.utils import StreamingQueryException 

bad_udf = udf(lambda x: 1 / 0) 

sq = sdf.select(bad_udf(col("value")))\ 

.writeStream\ 

.format('memory')\ 

.queryName('this_query')\ 

.start() 

try: 

# Process some data to fail the query 

sq.processAllAvailable() 

self.fail("bad udf should fail the query") 

except StreamingQueryException as e: 

# This is expected 

self._assert_exception_tree_contains_msg(e, "ZeroDivisionError") 

finally: 

sq.stop() 

self.assertTrue(type(sq.exception()) is StreamingQueryException) 

self._assert_exception_tree_contains_msg(sq.exception(), "ZeroDivisionError") 

 

def _assert_exception_tree_contains_msg(self, exception, msg): 

e = exception 

contains = msg in e.desc 

while e.cause is not None and not contains: 

e = e.cause 

contains = msg in e.desc 

self.assertTrue(contains, "Exception tree doesn't contain the expected message: %s" % msg) 

 

def test_query_manager_await_termination(self): 

df = self.spark.readStream.format('text').load('python/test_support/sql/streaming') 

250 ↛ 251line 250 didn't jump to line 251, because the loop on line 250 never started for q in self.spark._wrapped.streams.active: 

q.stop() 

tmpPath = tempfile.mkdtemp() 

shutil.rmtree(tmpPath) 

self.assertTrue(df.isStreaming) 

out = os.path.join(tmpPath, 'out') 

chk = os.path.join(tmpPath, 'chk') 

q = df.writeStream\ 

.start(path=out, format='parquet', queryName='this_query', checkpointLocation=chk) 

try: 

self.assertTrue(q.isActive) 

try: 

self.spark._wrapped.streams.awaitAnyTermination("hello") 

self.fail("Expected a value exception") 

except ValueError: 

pass 

now = time.time() 

# test should take at least 2 seconds 

res = self.spark._wrapped.streams.awaitAnyTermination(2.6) 

duration = time.time() - now 

self.assertTrue(duration >= 2) 

self.assertFalse(res) 

finally: 

q.processAllAvailable() 

q.stop() 

shutil.rmtree(tmpPath) 

 

class ForeachWriterTester: 

 

def __init__(self, spark): 

self.spark = spark 

 

def write_open_event(self, partitionId, epochId): 

self._write_event( 

self.open_events_dir, 

{'partition': partitionId, 'epoch': epochId}) 

 

def write_process_event(self, row): 

self._write_event(self.process_events_dir, {'value': 'text'}) 

 

def write_close_event(self, error): 

self._write_event(self.close_events_dir, {'error': str(error)}) 

 

def write_input_file(self): 

self._write_event(self.input_dir, "text") 

 

def open_events(self): 

return self._read_events(self.open_events_dir, 'partition INT, epoch INT') 

 

def process_events(self): 

return self._read_events(self.process_events_dir, 'value STRING') 

 

def close_events(self): 

return self._read_events(self.close_events_dir, 'error STRING') 

 

def run_streaming_query_on_writer(self, writer, num_files): 

self._reset() 

try: 

sdf = self.spark.readStream.format('text').load(self.input_dir) 

sq = sdf.writeStream.foreach(writer).start() 

for i in range(num_files): 

self.write_input_file() 

sq.processAllAvailable() 

finally: 

self.stop_all() 

 

def assert_invalid_writer(self, writer, msg=None): 

self._reset() 

try: 

sdf = self.spark.readStream.format('text').load(self.input_dir) 

sq = sdf.writeStream.foreach(writer).start() 

self.write_input_file() 

sq.processAllAvailable() 

self.fail("invalid writer %s did not fail the query" % str(writer)) # not expected 

except Exception as e: 

if msg: 

assert msg in str(e), "%s not in %s" % (msg, str(e)) 

 

finally: 

self.stop_all() 

 

def stop_all(self): 

for q in self.spark._wrapped.streams.active: 

q.stop() 

 

def _reset(self): 

self.input_dir = tempfile.mkdtemp() 

self.open_events_dir = tempfile.mkdtemp() 

self.process_events_dir = tempfile.mkdtemp() 

self.close_events_dir = tempfile.mkdtemp() 

 

def _read_events(self, dir, json): 

rows = self.spark.read.schema(json).json(dir).collect() 

dicts = [row.asDict() for row in rows] 

return dicts 

 

def _write_event(self, dir, event): 

import uuid 

with open(os.path.join(dir, str(uuid.uuid4())), 'w') as f: 

f.write("%s\n" % str(event)) 

 

def __getstate__(self): 

return (self.open_events_dir, self.process_events_dir, self.close_events_dir) 

 

def __setstate__(self, state): 

self.open_events_dir, self.process_events_dir, self.close_events_dir = state 

 

# Those foreach tests are failed in Python 3.6 and macOS High Sierra by defined rules 

# at http://sealiesoftware.com/blog/archive/2017/6/5/Objective-C_and_fork_in_macOS_1013.html 

# To work around this, OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES. 

def test_streaming_foreach_with_simple_function(self): 

tester = self.ForeachWriterTester(self.spark) 

 

def foreach_func(row): 

tester.write_process_event(row) 

 

tester.run_streaming_query_on_writer(foreach_func, 2) 

self.assertEqual(len(tester.process_events()), 2) 

 

def test_streaming_foreach_with_basic_open_process_close(self): 

tester = self.ForeachWriterTester(self.spark) 

 

class ForeachWriter: 

def open(self, partitionId, epochId): 

tester.write_open_event(partitionId, epochId) 

return True 

 

def process(self, row): 

tester.write_process_event(row) 

 

def close(self, error): 

tester.write_close_event(error) 

 

tester.run_streaming_query_on_writer(ForeachWriter(), 2) 

 

open_events = tester.open_events() 

self.assertEqual(len(open_events), 2) 

self.assertSetEqual(set([e['epoch'] for e in open_events]), {0, 1}) 

 

self.assertEqual(len(tester.process_events()), 2) 

 

close_events = tester.close_events() 

self.assertEqual(len(close_events), 2) 

self.assertSetEqual(set([e['error'] for e in close_events]), {'None'}) 

 

def test_streaming_foreach_with_open_returning_false(self): 

tester = self.ForeachWriterTester(self.spark) 

 

class ForeachWriter: 

def open(self, partition_id, epoch_id): 

tester.write_open_event(partition_id, epoch_id) 

return False 

 

def process(self, row): 

tester.write_process_event(row) 

 

def close(self, error): 

tester.write_close_event(error) 

 

tester.run_streaming_query_on_writer(ForeachWriter(), 2) 

 

self.assertEqual(len(tester.open_events()), 2) 

 

self.assertEqual(len(tester.process_events()), 0) # no row was processed 

 

close_events = tester.close_events() 

self.assertEqual(len(close_events), 2) 

self.assertSetEqual(set([e['error'] for e in close_events]), {'None'}) 

 

def test_streaming_foreach_without_open_method(self): 

tester = self.ForeachWriterTester(self.spark) 

 

class ForeachWriter: 

def process(self, row): 

tester.write_process_event(row) 

 

def close(self, error): 

tester.write_close_event(error) 

 

tester.run_streaming_query_on_writer(ForeachWriter(), 2) 

self.assertEqual(len(tester.open_events()), 0) # no open events 

self.assertEqual(len(tester.process_events()), 2) 

self.assertEqual(len(tester.close_events()), 2) 

 

def test_streaming_foreach_without_close_method(self): 

tester = self.ForeachWriterTester(self.spark) 

 

class ForeachWriter: 

def open(self, partition_id, epoch_id): 

tester.write_open_event(partition_id, epoch_id) 

return True 

 

def process(self, row): 

tester.write_process_event(row) 

 

tester.run_streaming_query_on_writer(ForeachWriter(), 2) 

self.assertEqual(len(tester.open_events()), 2) # no open events 

self.assertEqual(len(tester.process_events()), 2) 

self.assertEqual(len(tester.close_events()), 0) 

 

def test_streaming_foreach_without_open_and_close_methods(self): 

tester = self.ForeachWriterTester(self.spark) 

 

class ForeachWriter: 

def process(self, row): 

tester.write_process_event(row) 

 

tester.run_streaming_query_on_writer(ForeachWriter(), 2) 

self.assertEqual(len(tester.open_events()), 0) # no open events 

self.assertEqual(len(tester.process_events()), 2) 

self.assertEqual(len(tester.close_events()), 0) 

 

def test_streaming_foreach_with_process_throwing_error(self): 

from pyspark.sql.utils import StreamingQueryException 

 

tester = self.ForeachWriterTester(self.spark) 

 

class ForeachWriter: 

def process(self, row): 

raise RuntimeError("test error") 

 

def close(self, error): 

tester.write_close_event(error) 

 

try: 

tester.run_streaming_query_on_writer(ForeachWriter(), 1) 

self.fail("bad writer did not fail the query") # this is not expected 

except StreamingQueryException as e: 

# TODO: Verify whether original error message is inside the exception 

pass 

 

self.assertEqual(len(tester.process_events()), 0) # no row was processed 

close_events = tester.close_events() 

self.assertEqual(len(close_events), 1) 

# TODO: Verify whether original error message is inside the exception 

 

def test_streaming_foreach_with_invalid_writers(self): 

 

tester = self.ForeachWriterTester(self.spark) 

 

def func_with_iterator_input(iter): 

for x in iter: 

print(x) 

 

tester.assert_invalid_writer(func_with_iterator_input) 

 

class WriterWithoutProcess: 

def open(self, partition): 

pass 

 

tester.assert_invalid_writer(WriterWithoutProcess(), "does not have a 'process'") 

 

class WriterWithNonCallableProcess(): 

process = True 

 

tester.assert_invalid_writer(WriterWithNonCallableProcess(), 

"'process' in provided object is not callable") 

 

class WriterWithNoParamProcess(): 

def process(self): 

pass 

 

tester.assert_invalid_writer(WriterWithNoParamProcess()) 

 

# Abstract class for tests below 

class WithProcess(): 

def process(self, row): 

pass 

 

class WriterWithNonCallableOpen(WithProcess): 

open = True 

 

tester.assert_invalid_writer(WriterWithNonCallableOpen(), 

"'open' in provided object is not callable") 

 

class WriterWithNoParamOpen(WithProcess): 

def open(self): 

pass 

 

tester.assert_invalid_writer(WriterWithNoParamOpen()) 

 

class WriterWithNonCallableClose(WithProcess): 

close = True 

 

tester.assert_invalid_writer(WriterWithNonCallableClose(), 

"'close' in provided object is not callable") 

 

def test_streaming_foreachBatch(self): 

q = None 

collected = dict() 

 

def collectBatch(batch_df, batch_id): 

collected[batch_id] = batch_df.collect() 

 

try: 

df = self.spark.readStream.format('text').load('python/test_support/sql/streaming') 

q = df.writeStream.foreachBatch(collectBatch).start() 

q.processAllAvailable() 

self.assertTrue(0 in collected) 

self.assertTrue(len(collected[0]), 2) 

finally: 

551 ↛ exitline 551 didn't return from function 'test_streaming_foreachBatch', because the condition on line 551 was never false if q: 

q.stop() 

 

def test_streaming_foreachBatch_propagates_python_errors(self): 

from pyspark.sql.utils import StreamingQueryException 

 

q = None 

 

def collectBatch(df, id): 

raise RuntimeError("this should fail the query") 

 

try: 

df = self.spark.readStream.format('text').load('python/test_support/sql/streaming') 

q = df.writeStream.foreachBatch(collectBatch).start() 

q.processAllAvailable() 

self.fail("Expected a failure") 

except StreamingQueryException as e: 

self.assertTrue("this should fail" in str(e)) 

finally: 

570 ↛ exitline 570 didn't return from function 'test_streaming_foreachBatch_propagates_python_errors', because the condition on line 570 was never false if q: 

q.stop() 

 

def test_streaming_read_from_table(self): 

with self.table("input_table", "this_query"): 

self.spark.sql("CREATE TABLE input_table (value string) USING parquet") 

self.spark.sql("INSERT INTO input_table VALUES ('aaa'), ('bbb'), ('ccc')") 

df = self.spark.readStream.table("input_table") 

self.assertTrue(df.isStreaming) 

q = df.writeStream.format('memory').queryName('this_query').start() 

q.processAllAvailable() 

q.stop() 

result = self.spark.sql("SELECT * FROM this_query ORDER BY value").collect() 

self.assertEqual( 

set([Row(value='aaa'), Row(value='bbb'), Row(value='ccc')]), set(result)) 

 

def test_streaming_write_to_table(self): 

with self.table("output_table"), tempfile.TemporaryDirectory() as tmpdir: 

df = self.spark.readStream.format("rate").option("rowsPerSecond", 10).load() 

q = df.writeStream.toTable("output_table", format='parquet', checkpointLocation=tmpdir) 

self.assertTrue(q.isActive) 

time.sleep(3) 

q.stop() 

result = self.spark.sql("SELECT value FROM output_table").collect() 

self.assertTrue(len(result) > 0) 

 

 

if __name__ == "__main__": 

import unittest 

from pyspark.sql.tests.test_streaming import * # noqa: F401 

 

try: 

import xmlrunner # type: ignore[import] 

testRunner = xmlrunner.XMLTestRunner(output='target/test-reports', verbosity=2) 

except ImportError: 

testRunner = None 

unittest.main(testRunner=testRunner, verbosity=2)