我在Python3中运行了一些单元测试,但print()似乎没有用。
以下是我的代码片段:
import unittest
def lexer(line):
... #Lots of code and print statements here
return tokens
class lexerBasicTestCases(unittest.TestCase):
def Test1(self):
test = "9+4"
output = ["9", "+", "4"]
self.assertEqual(lexer(test), output)
def Test2(self):
test = "9 + 4"
output = ["9", "+", "4"]
self.assertEqual(lexer(test), output)
def Test2(self):
test = "9 + 4"
output = ["9", "+", "4"]
self.assertEqual(lexer(test), output)
def lexerBasicTestSuite():
suite = unittest.TestSuite()
suite.addTest(lexerBasicTestCases('Test1'))
suite.addTest(lexerBasicTestCases('Test2'))
suite.addTest(lexerBasicTestCases('Test3'))
unittest.TextTestRunner(verbosity=2).run(suite)
#Main program
lexerBasicTestSuite()
我使用:
运行我的代码python \interpreter.py
运行单元测试时,将忽略lexer()内的print()语句。当测试用例失败并且我没有要分析的打印语句时,这是令人沮丧的。当我调试时,我宁愿不注释单元测试并编写一个单独运行测试的对应函数。如何打印出打印报告?
答案 0 :(得分:2)
单元测试并行运行,因此您不能依赖打印功能,因为它不是线程安全的。改为使用记录器,甚至更好地将自定义消息传递给断言方法。
import { Component } from '@angular/core';
import { PretragaService } from '../services/pretraga.service';
@Component({
selector: 'pretraga',
template: `
<input type="text" class="form-control" placeholder="pretraži"
[(ngModel)]="searchJoke"
name="searchJoke" (keyup)="searchJokes()">
<div *ngIf="searchRes">
<div *ngFor="let joke of searchRes">
<ul>
<li>{{joke}}</li>
</ul>
</div>
</div>
`,
providers: [PretragaService]
})
export class PretragaComponent {
searchJoke: string;
searchRes: any[]=[];
constructor(private searchService: PretragaService){
}
searchJokes(){
this.searchService.searchJokes(this.searchJoke)
.subscribe(joke => {
this.searchRes = joke;
});
}