Python to run a single test?

I'm looking for something like ruby ​​rspec metadata focus

or elixir mix tags to run a single python test.

Ruby RSpec example:

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

      

Elixir ExUnit and Mix example:

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

      

Is this possible / available with any test runner and combination of python tools? Running single tests by specifying nested module.class.test_name

args via the command line can get very verbose in larger projects.

So something like:

Required Python code:

# $ nosetests --only focus

from tests.fixtures import focus

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

      

+3


source to share


1 answer


Say hello to pytest mark . You can create a focus tag, assign any test case or method, and then run the tests using the command pytest -v -m focus

. For example:

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)

      



will be executed test_method2

. To run all methods within a TestCase, you simply mark the class:

import unittest
import pytest

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

      

+4


source







All Articles