diff --git a/requirements.txt b/requirements.txt index d584511a7d523ac006fa50726e706263179173ef..377628829973d2c44649b009e86c1e8a0fd802d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ fabric>=2.5.0 -pytest>=4.6.9 -allure-pytest>=2.8.11 \ No newline at end of file +pytest==4.6.9 +allure-pytest>=2.8.11 +pytest-rerunfailures==8.0 \ No newline at end of file diff --git a/run.py b/run.py index d3246c4ddecd7946fbaf244c22afda8eb0c2dd96..4dd970ec4afcf3bed41897b021c220f2a758ba24 100644 --- a/run.py +++ b/run.py @@ -5,20 +5,80 @@ import pytest import os import argparse import sys +import logging.config +from configobj import ConfigObj +# 设置conf.ini路径 +currentdir = os.path.dirname(__file__) +logging.config.fileConfig("logging.ini") +logger = logging.getLogger('fileAndConsole') + + + + +def json_handle(js_str,serverip,act): + if act != '运行': + logging.warning('未指定执行,请确认<运行>是否勾选') + sys.exit(0) + for i in eval(js_str): + if serverip == i['1server']: + return i + return None + +def modify_conf(temp): + config = ConfigObj("settings.ini",encoding='UTF8') + config['Server']['host'] = temp['1server'] + config['Server']['user'] = temp['2user'] + config['Server']['password'] = temp['3password'] + config.write() + + + +def allcase(): + temp = [] + for i in module_dict: + if isinstance(module_dict[i],list): + temp.extend(module_dict[i]) + else: + temp.append(module_dict[i]) + return temp + +def fullCaseName(tempList): + newList = [] + for i in tempList: + newList.append('testCase/test_' + i + '.py') + + return newList + +def pytest_command(modules): + run_module = modules['4module'] + suite = [] + if 'all' in run_module: + suite = allcase() + return fullCaseName(suite) + else: + for key in module_dict: + if key in run_module: + if isinstance(module_dict[key],list): + suite.extend(module_dict[key]) + else: + suite.append(module_dict[key]) + return fullCaseName(suite) if __name__ == '__main__': - module_list = [ 'all', - '系统基本功能测试' - ,'性能测试'] - submodule_list = ['all', - "基本命令", - "扩展命令"] - - a = sys.argv[1] - if a == '基本命令': - print 'a' - pytest.main(['-s','--alluredir','./result']) + module_dict = { + '基本命令':['specialCmd','usualCmd'], + '性能测试':'performance' + } + + js_serverinfo = sys.argv[1] + run_server = sys.argv[2] + action = sys.argv[3] + serverinfo = json_handle(js_serverinfo,run_server,action) + modify_conf(serverinfo) + pytest_argu = ['-s','--reruns=2','--alluredir', '../result'] + pytest_argu[1:1] = pytest_command(serverinfo) + pytest.main(pytest_argu) #os.system(r"allure generate --clean allure-results -o allure-report") \ No newline at end of file diff --git a/sh/time.sh b/sh/time.sh new file mode 100644 index 0000000000000000000000000000000000000000..333e01fd0edbbd413107cfa168dc2301bcd3a262 --- /dev/null +++ b/sh/time.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +time date \ No newline at end of file diff --git a/testCase/conftest.py b/testCase/conftest.py index a0a769b1edeb87f9af0fa494a646f449fea26bb3..6ab7f022798860aabdf873e4622c8409379ee80c 100644 --- a/testCase/conftest.py +++ b/testCase/conftest.py @@ -7,7 +7,7 @@ sys.setdefaultencoding('utf8') sys.path.append('./') import pytest -from _util.common import Conf,SSH_Conn,Eth,randstr +from util.common import Conf,SSH_Conn,Eth,randstr @pytest.fixture(scope="session") def init(): diff --git a/testCase/test_performance.py b/testCase/test_performance.py new file mode 100644 index 0000000000000000000000000000000000000000..e3f81d66f815bc5eb96bf8fc38ed89b0195fdde3 --- /dev/null +++ b/testCase/test_performance.py @@ -0,0 +1,20 @@ +#-*- coding: utf-8 -*- +#!/usr/bin/python + +import sys, os +sys.path.append(os.path.abspath('././linuxtest')) +import allure +import pytest +import logging + + +class Test_performance(): + + def test_1001(self,init): + ''' + test + ''' + assert True + +if __name__ == "__main__": + pytest.main(['-s']) \ No newline at end of file diff --git a/testCase/test_usualCmd.py b/testCase/test_usualCmd.py index 39bde0feb02694d09d0756c030d5968538e9fdd7..e784f49d731dd3abf8ceaea0cb7701595c366ba2 100644 --- a/testCase/test_usualCmd.py +++ b/testCase/test_usualCmd.py @@ -7,12 +7,14 @@ import allure import pytest import logging import invoke -from _util.common import Conf,SSH_Conn,Eth,randstr +from util.common import Conf,SSH_Conn,Eth,randstr +import re + class Test_commonCmd(): - def test_48614(self,init,tempspace): + def test_zcat(self,init,tempspace): ''' zcat ''' @@ -24,7 +26,7 @@ class Test_commonCmd(): assert ori == now logging.info('\r\n'+'-'*20 +'\r\n' + now +'-'*20) - def test_48613(self,init): + def test_xargs(self,init): ''' xargs ''' @@ -33,7 +35,7 @@ class Test_commonCmd(): assert result.ok logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) - def test_48612(self,init): + def test_wc(self,init): ''' wc ''' @@ -42,9 +44,10 @@ class Test_commonCmd(): assert result.ok logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) - def test_48609_11(self,init): + def test_usermod_add_del(self,init): ''' usermod + useradd userdel ''' c = init @@ -53,7 +56,7 @@ class Test_commonCmd(): logging.info('-'*20) userid = c.cmd("cat /etc/passwd |grep %s|awk -F ':' '{print $3}'" % user).stdout assert userid != None, 'useradd failed' - #删除1005uid用户 + #删除2005uid用户 needdel = c.cmd("cat /etc/passwd |grep x:2005:| awk -F ':' '{print $1}'").stdout.strip() if needdel != '': c.sudo_cmd('userdel %s' % needdel) @@ -66,7 +69,7 @@ class Test_commonCmd(): assert a != None, 'userdel failed' logging.info('-'*20) - def test_48608(self,init): + def test_uniq(self,init): ''' uniq ''' @@ -84,7 +87,7 @@ class Test_commonCmd(): c.cmd('rm -f %s' % text) logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) - def test_48607(self,init): + def test_unexpand(self,init): ''' unexpand ''' @@ -97,7 +100,7 @@ class Test_commonCmd(): c.cmd('rm -f %s' % text) logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) - def test_48606(self,init): + def test_uname(self,init): ''' uname ''' @@ -106,7 +109,7 @@ class Test_commonCmd(): assert result.stdout.strip() == 'Linux' logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) - def test_48604(self,init): + def test_tty(self,init): ''' tty ''' @@ -116,33 +119,34 @@ class Test_commonCmd(): logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) - def test_48603(self,init): + def test_tsort(self,init): ''' tsort ''' c = init text = randstr(4)+'.txt' - c.cmd('echo "aaa\r\nccc\r\nddd\r\nzzz\r\nrrr\r\nmmm\r\niii\r\nppp" >> %s' % text) + c.cmd('echo "aaa\r\nccc\r\nddd\r\nzzz\r\nttt\r\nmmm\r\niii\r\nppp" >> %s' % text) result = c.cmd('tsort %s' % text) - assert result.stdout.strip(' ') == 'aaadddiiirrrccczzzpppmmm' + result = result.stdout.strip(' ') + assert result.replace('\n', '').replace('\r', '') == 'aaadddiiitttccczzzpppmmm' c.cmd('rm -f %s' % text) - logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) + logging.info('\r\n'+'-'*20 +'\r\n' + result +'-'*20) - def test_48602(self,init): + def test_TRUE(self,init): ''' TRUE ''' c = init conf = Conf() usrname = conf.getconf("Server").user - c.conn.put('./linuxtest/sh/true.sh','/home/%s/true.sh' % usrname) + c.conn.put('./sh/true.sh','/home/%s/true.sh' % usrname) c.sudo_cmd('chmod +x true.sh') result = c.cmd('/home/%s/true.sh' % usrname) assert result.stdout.strip() == 'a等于b' logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) c.cmd('rm -f true.sh') - def test_48601(self,init): + def test_tr(self,init): ''' tr ''' @@ -151,6 +155,50 @@ class Test_commonCmd(): assert result.stdout.strip() == 'hello world' logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) + def test_touch(self,init): + ''' + touch + ''' + c = init + text = randstr(4)+'.txt' + c.cmd('touch %s' % text) + result = c.cmd('ls -l %s' % text).stdout + date = c.cmd('date -R').stdout + result = re.search(r'\d.:\d.',result) + date = re.search(r'\d.:\d.',date) + assert result.group(0) == date.group(0) + logging.info('\r\n'+'-'*20 +'\r\n' + result.group(0)+'\r\n'+ date.group(0)+'-'*20) + c.cmd('rm -f %s' % text) + + + def test_time(self,init): + ''' + time + ''' + c = init + conf = Conf() + usrname = conf.getconf("Server").user + c.conn.put('./sh/time.sh','/home/%s/time.sh' % usrname) + c.sudo_cmd('chmod +x time.sh') + result = c.cmd('/home/%s/time.sh' % usrname) + flag = re.search(r'\dm.+s', result.stdout) + if flag == None: + assert False, '没有打印文件时间' + else: + assert True + logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) + c.cmd('rm -f time.sh') + + def test_ls(self,init): + ''' + ls + ''' + c=init + result = c.cmd('ls') + assert result.ok + logging.info('\r\n'+'-'*20 +'\r\n' + result.stdout +'-'*20) + + if __name__ == "__main__": pytest.main(['-s']) diff --git a/_util/__init__.py b/util/__init__.py similarity index 100% rename from _util/__init__.py rename to util/__init__.py diff --git a/_util/common.py b/util/common.py similarity index 96% rename from _util/common.py rename to util/common.py index 0c26bec9fdba70b5156883a1631963aa194ca4be..3302c6dd17ae0f1aa0796ab7e5dcc191101cd7f2 100644 --- a/_util/common.py +++ b/util/common.py @@ -12,11 +12,6 @@ import logging.config import pytest -# 设置conf.ini路径 -currentdir = os.path.dirname(__file__) -logging.config.fileConfig(currentdir+"/logging.ini") -logger = logging.getLogger('fileAndConsole') - class Dictionary(dict): ''' diff --git a/_util/logging.ini b/util/logging.ini similarity index 100% rename from _util/logging.ini rename to util/logging.ini