编程语言
首页 > 编程语言> > Python测试夹具运行单个测试?

Python测试夹具运行单个测试?

作者:互联网

我正在寻找像ruby rspec的焦点元数据或elixir的mix标签来运行单个python测试.

Ruby RSpec示例:

# $rspec spec
it 'runs a single test', :focus do 
  expect(2).to eq(2)
end

Elixir ExUnit&混合示例:

# $mix test --only focus
@tag :focus
test "only run this test" do
  assert true
end

这可能/任何python测试运行器和夹具组合可用吗?通过命令行参数指定嵌套的module.class.test_name来运行单个测试在大型项目中会变得非常冗长.

所以像:

期望的Python代码:

# $nosetests --only focus

from tests.fixtures import focus

class TestSomething(unittest.TestCase):
    @focus
    def test_all_the_things(self):
        self.assertEqual(1, 1)

解决方法:

pytest mark问好.您可以创建焦点标记,分配给任何测试用例或方法,然后使用pytest -v -m focus命令运行测试.例如:

import unittest
import pytest


class TestOne(unittest.TestCase):
    def test_method1(self):
        # I won't be executed with focus mark
        self.assertEqual(1, 1)

    @pytest.mark.focus
    def test_method2(self):  
        # I will be executed with focus mark          
        self.assertEqual(1, 1)

将运行test_method2.要在某些TestCase中运行所有方法,只需标记一个类:

import unittest
import pytest

@pytest.mark.focus
class TestOne(unittest.TestCase):
    ...

标签:python,unit-testing,nose,pytest
来源: https://codeday.me/bug/20190828/1751226.html