LoginSignup
0
1

More than 3 years have passed since last update.

pytest

Posted at

calculation.pyをpytestでテストします

calculation.py
import os

class Cal(object):
    def add_num_and_double(self, x, y):

        if type(x) is not int or type(y) is not int:
            raise ValueError
        result = x + y
        result *= 2
        return result

    def save(self, dir_path, file_name):
        if not os.path.exists(dir_path):
            os.mkdir(dir_path)
        file_path = os.path.join(dir_path, file_name)
        with open(file_path, 'w') as f:
            f.write('test')
conftest.py
import os
import pytest


#独自のfixture
@pytest.fixture
def csv_file(tmpdir):
    with open(os.path.join(tmpdir, 'test.csv'), 'w+') as c:
        print('before test')
        yield c
        print('after test')


def pytest_addoption(parser):
    parser.addoption('--os-name', default='linux', help='os name')
test_calculation.py
#pip install pytest
import pytest
import calculation
import os

is_release = True
#関数でpytestする場合、先頭にtest_付ける
#def test_add_num_and_double():
#    cal = calculation.Cal()
#    assert cal.add_num_and_double(1, 1) != 4

#クラスでpytestする場合、先頭にTEST付ける
class TestCal(object):

    @classmethod
    def setup_class(cls):
        print('start')
        cls.cal = calculation.Cal()
        cls.test_dir = '/tmp/test_dir'
        cls.test_file_name = 'test.txt'

    def test_save_no_dir(self):
        self.cal.save(self.test_dir, self.test_file_name)
        test_file_path = os.path.join(self.test_dir, self.test_file_name)
        assert os.path.exists(test_file_path) is True

    @classmethod
    def teardown_class(cls):
        print('end')
        del cls.cal
        #本当は上に書く
        import shutil
        if os.path.exists(cls.test_dir):
            shutil.rmtree(cls.test_dir)

    # テストが走る前に呼ばれる
    def setup_method(self, method):
        print('method={}'.format(method.__name__))
        #self.cal = calculation.Cal()

    # テスト終了後に呼ばれる
    def teardown_method(self, method):
        print('method={}'.format(method.__name__))
        #del self.cal

    #csv_fileが独自のfixture
    def test_add_num_and_double(self, csv_file):
        print(csv_file)
        #fixtureがrequestの場合
        #os_name = request.config.getoption('--os-name')
        #print(os_name)
        #if os_name == 'mac':
        #    print('ls')
        #elif os_name == 'windows':
            #print('dir')

        #cal = calculation.Cal()
        assert self.cal.add_num_and_double(1, 1) == 4

    def test_save(self, tmpdir):
        self.cal.save(tmpdir, self.test_file_name)
        test_file_path = os.path.join(tmpdir, self.test_file_name)
        assert os.path.exists(test_file_path) is True

    #例外テスト(スキップも可能)
    #@pytest.mark.skip(reason='skip!')
    @pytest.mark.skipif(is_release==True, reason='skip!')
    def test_add_num_and_double_raise(self):
        with pytest.raises(ValueError):
            #cal = calculation.Cal()
            self.cal.add_num_and_double('1', '1')

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1