1+ //
2+ // Note: This example test is leveraging the Mocha test framework.
3+ // Please refer to their documentation on https://mochajs.org/ for help.
4+ //
5+ // Place this right on top
6+ import { initialize } from './initialize' ;
7+
8+ // The module \'assert\' provides assertion methods from node
9+ import * as assert from 'assert' ;
10+
11+ // You can import and use all API from the \'vscode\' module
12+ // as well as import your extension to test it
13+ import * as vscode from 'vscode' ;
14+ import { Tests , TestsToRun , TestFolder , TestFile , TestStatus , TestSuite , TestFunction , FlattenedTestFunction , CANCELLATION_REASON } from '../client/unittests/common/contracts' ;
15+ import * as nosetests from '../client/unittests/nosetest/main' ;
16+ import * as pytest from '../client/unittests/pytest/main' ;
17+ import * as unittest from '../client/unittests/unittest/main' ;
18+ import { resolveValueAsTestToRun , getDiscoveredTests } from '../client/unittests/common/testUtils' ;
19+ import { BaseTestManager } from '../client/unittests/common/baseTestManager' ;
20+ import { PythonSettings , IUnitTestSettings } from '../client/common/configSettings' ;
21+ import { TestResultDisplay } from '../client/unittests/display/main' ;
22+ import { TestDisplay } from '../client/unittests/display/picker' ;
23+ import * as constants from '../client/common/constants' ;
24+ import { activateCodeLenses } from '../client/unittests/codeLenses/main' ;
25+
26+ let testManager : BaseTestManager ;
27+ let pyTestManager : pytest . TestManager ;
28+ let unittestManager : unittest . TestManager ;
29+ let nosetestManager : nosetests . TestManager ;
30+ let testResultDisplay : TestResultDisplay ;
31+ let testDisplay : TestDisplay ;
32+ let outChannel : vscode . OutputChannel ;
33+ let lastRanTests : TestsToRun = null ;
34+
35+ import * as path from 'path' ;
36+ import * as configSettings from '../client/common/configSettings' ;
37+ import * as fs from 'fs-extra' ;
38+ import { execPythonFile } from '../client/common/utils' ;
39+
40+ let pythonSettings = configSettings . PythonSettings . getInstance ( ) ;
41+ const settings = PythonSettings . getInstance ( ) ;
42+
43+ const UNITTEST_TEST_FILES_PATH = path . join ( __dirname , '..' , '..' , 'src' , 'test' , 'pythonFiles' , 'unitests' ) ;
44+ class MockOutputChannel implements vscode . OutputChannel {
45+ constructor ( name : string ) {
46+ this . name = name ;
47+ this . output = '' ;
48+ }
49+ name : string ;
50+ output : string ;
51+ append ( value : string ) {
52+ this . output += value ;
53+ }
54+ appendLine ( value : string ) { this . append ( value ) ; this . append ( '\n' ) ; }
55+ clear ( ) { }
56+ show ( preservceFocus ?: boolean ) : void ;
57+ show ( column ?: vscode . ViewColumn , preserveFocus ?: boolean ) : void ;
58+ show ( x ?: any , y ?: any ) : void { }
59+ hide ( ) { }
60+ dispose ( ) { }
61+ }
62+
63+ suiteSetup ( done => {
64+ initialize ( ) . then ( ( ) => {
65+ done ( ) ;
66+ } ) ;
67+ } ) ;
68+ suiteTeardown ( done => {
69+ done ( ) ;
70+ } ) ;
71+
72+ suite ( 'Unit Tests (unittest)' , ( ) => {
73+ // setup(() => {
74+ // pythonSettings.unitTest.unittestEnabled = true;
75+ // pythonSettings.unitTest.nosetestsEnabled = false;
76+ // pythonSettings.unitTest.pyTestEnabled = false;
77+ // outChannel = new MockOutputChannel('Python Test Log');
78+ // });
79+
80+ test ( 'Discover Tests' , ( ) => {
81+ const rootDirectory = UNITTEST_TEST_FILES_PATH ;
82+ pythonSettings . unitTest . unittestArgs = [
83+ '-s=./tests' ,
84+ '-p=test_*.py'
85+ ] ;
86+ const testManager = unittestManager ? unittestManager : new unittest . TestManager ( rootDirectory , outChannel ) ;
87+ if ( ( testManager . status !== TestStatus . Discovering && testManager . status !== TestStatus . Running ) ) {
88+ testResultDisplay = testResultDisplay ? testResultDisplay : new TestResultDisplay ( outChannel ) ;
89+ testManager . discoverTests ( true , true ) . then ( tests => {
90+ assert . equal ( tests . testFiles . length , 2 , 'Incorrect number of test files' ) ;
91+ assert . equal ( tests . testFunctions . length , 6 , 'Incorrect number of test functions' ) ;
92+ assert . equal ( tests . testSuits . length , 3 , 'Incorrect number of test suites' ) ;
93+ assert . equal ( tests . testFiles . some ( t => t . name === 'test_unittest_one.py' && t . nameToRun === 'Test_test1.test_A' ) , true , 'Test File not found' ) ;
94+ assert . equal ( tests . testFiles . some ( t => t . name === 'test_unittest_two.py' && t . nameToRun === 'Test_test2.test_A2' ) , true , 'Test File not found' ) ;
95+ } ) . catch ( reason => {
96+ assert . fail ( reason , null , 'Test Discovery failed' , '' ) ;
97+ } ) ;
98+ }
99+ } ) ;
100+
101+ test ( 'Discover Tests (pattern = *_test_*.py)' , ( ) => {
102+ const rootDirectory = UNITTEST_TEST_FILES_PATH ;
103+ pythonSettings . unitTest . unittestArgs = [
104+ '-s=./tests' ,
105+ '-p=*_test*.py'
106+ ] ;
107+ const testManager = unittestManager ? unittestManager : new unittest . TestManager ( rootDirectory , outChannel ) ;
108+ if ( ( testManager . status !== TestStatus . Discovering && testManager . status !== TestStatus . Running ) ) {
109+ testResultDisplay = testResultDisplay ? testResultDisplay : new TestResultDisplay ( outChannel ) ;
110+ testManager . discoverTests ( true , true ) . then ( tests => {
111+ assert . equal ( tests . testFiles . length , 1 , 'Incorrect number of test files' ) ;
112+ assert . equal ( tests . testFunctions . length , 2 , 'Incorrect number of test functions' ) ;
113+ assert . equal ( tests . testSuits . length , 1 , 'Incorrect number of test suites' ) ;
114+ assert . equal ( tests . testFiles . some ( t => t . name === 'unittest_three_test.py' && t . nameToRun === 'Test_test3.test_A' ) , true , 'Test File not found' ) ;
115+ } ) . catch ( reason => {
116+ assert . fail ( reason , null , 'Test Discovery failed' , '' ) ;
117+ } ) ;
118+ }
119+ } ) ;
120+ } ) ;
0 commit comments