在Jetbrains Idea或Pycharm中运行选择性单元测试

huangapple go评论121阅读模式
英文:

Running Selective Unit Tests in Jetbrains Idea or Pycharm

问题

我有一个包含多个测试方法的测试套件,但我想要只运行特定的测试方法(即test_one),使用Jetbrains产品(例如Pycharm、Idea)。在这种情况下,我尝试避免使用命令行方法。

我该如何做?

以下是单元测试的代码示例:

import unittest

class TestMyMethods(unittest.TestCase):
    def setUp(self):
        # 在运行每个测试方法之前设置必要的状态
        pass

    def test_one(self):
        # 测试方法
        print('Running test: one')

    def test_two(self):
        # 测试方法
        print('Running test: two')

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(TestMyMethods('test_one'))
    unittest.TextTestRunner().run(suite)

运行上述代码将生成以下输出:

Testing started at 1:09 PM ...
Launching unittests with arguments python -m unittest C:\Users\mpx\so.py in C:\Users\Users

Running test: one

Ran 1 test in 0.001s

OK

Process finished with exit code 0
英文:

I have a test suite with multiple test methods, but I want to run only a specific test method (i.e., test_one) using Jetbrains product (e.g., Pycharm, Idea). In this case, I try to avoid using the command line approach.

How can I do that?

The unit test is as below:

import unittest

class TestMyMethods(unittest.TestCase):
    def setUp(self):
        # set up any necessary state before each test method is run
        pass

    def test_one(self):
        # test method
        print('Running test: one')

    def test_two(self):
        # test method
        print('Running test: two')


if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(TestMyMethods('test_one'))
    unittest.TextTestRunner().run(suite)

Running the above code generate the following

Testing started at 1:09 PM ...
Launching unittests with arguments python -m unittest C:\Users\mpx\so.py in C:\Users\Users

Running test: one
Running test: two


Ran 2 tests in 0.002s

OK

Process finished with exit code 0

答案1

得分: 1

在PyCharm中,你的每个测试应该在左侧的行号旁边有一个绿色三角形(这被称为gutter)。只需点击你想要的测试的那个。

注意:你可以通过搜索“pycharm运行测试”并点击第一个链接,即由JetBrains编写的文档,来找到更多详细信息。

英文:

In PyCharm, each of your tests should have a green triangle on the left next to the line numbers (this is called the gutter). Just click on the one for the test you want.

Note: you can find more details by googling "pycharm run tests" and clicking on the first link which is the documentation written by JetBrains.

huangapple
  • 本文由 发表于 2023年3月12日 13:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定