Artificial Intelligence 14 min read

Automated APK Test Script Recommendation: Data Processing and Model Training Pipeline

This article describes a complete pipeline for recommending automated test scripts for APK releases, covering CSV data preprocessing, feature encoding, tokenization with pkuseg and jieba, and training various machine‑learning models such as LDA, word2vec, XGBoost, deep neural networks, and multi‑label classifiers to predict script execution order.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Automated APK Test Script Recommendation: Data Processing and Model Training Pipeline

During APK testing, automated scripts can reduce manual effort, but as APKs evolve the number of scripts grows, increasing maintenance cost. A precise testing approach extracts code differences between successive builds, uses Android code coverage data to train a model that maps these differences to a recommended execution order of automation scripts.

Overall Model Flow

The solution consists of five parts: data file reception service, data file processing, model training, model prediction, and storage mode. The article focuses on the three core functions: data processing, model training, and model prediction.

Data File Processing

The raw data is a CSV file containing fields such as caseId, className, funcName, funcDesc, lineCode, etc. Each column is transformed into numeric values (≥1) with identical original values receiving the same number, followed by min‑max normalization; zero indicates a missing type.

Key processing steps include:

Splitting full class names into className and packageName, treating packageName as an additional feature.

Encoding method parameters into a fixed‑length integer array without duplicates, adding a placeholder for unseen types.

Tokenizing lineCode using both pkuseg and jieba (or jieba_fast) after removing stop words.

Example code for column encoding and tokenization:

# caseId
caseId_dict = cvsData['caseId'].unique().tolist()
cvsData['caseId_value'] = cvsData['caseId'].apply(lambda x: caseId_dict.index(x) + 1)

# className
className_dict = cvsData['className'].unique().tolist()
cvsData['className_value'] = cvsData['className'].apply(lambda x: className_dict.index(x) + 1)

# packageName
cvsData['packageName'] = cvsData['className'].str.rsplit('/', 1).str[0]
packageName_dict = cvsData['packageName'].unique().tolist()
cvsData['packageName_value'] = cvsData['packageName'].apply(lambda x: packageName_dict.index(x) + 1)

# lineCode tokenization (pkuseg)
from pkuseg import pkuseg
seg = pkuseg(pkuseg.model_name="default")
stopwords = set(open(common.stop_words, 'r', encoding='UTF-8').readlines())

def movestopwords(sentence):
    return [w for w in sentence if len(w) > 1 and w not in stopwords]

def participle(linecode):
    words = seg.cut(linecode.lower().strip())
    return movestopwords(words)

cvsData['lineCode_word'] = cvsData['lineCode'].apply(participle)

Model Training

Before training, the processed data is split into a feature vector and a label vector. Several models are explored:

LDA model : Treats tokenized arrays as documents, sets the number of topics to 8, and generates an 8‑dimensional topic vector for each line of code.

Word2Vec model : Trains 50‑dimensional embeddings for each token and averages them to obtain a line‑level vector.

Feature merging : Combines className, packageName, funcName, funcReturn, funcParameter, and the LDA/Word2Vec vectors into a single high‑dimensional feature matrix.

Label generation : Handles cases where identical code snippets map to multiple test cases by creating a multi‑label representation based on MD5 hashes of the feature rows.

Classification models : XGBoost (multi‑class softprob) with depth=2, learning_rate=0.25, 40 trees. Deep Neural Network built with Keras (two hidden layers of 128 units, ReLU activation, softmax output). True multi‑label classifier using OneVsRest SVC after transforming labels with MultiLabelBinarizer.

Example code for XGBoost training:

# Get labels
labels = CvsData['new_Larber_value']
XGBTData = np.array(XGBTData)
num_class = len(CvsData['new_Larber_value'].drop_duplicates())
classifier = XGBClassifier(max_depth=2, learning_rate=0.25, n_estimators=40,
                          subsample=0.9, colsample_bytree=0.9,
                          objective='multi:softprob', num_class=num_class)
classifier.fit(XGBTData, labels, eval_set=[(XGBTData, labels)],
               eval_metric='merror', early_stopping_rounds=20, verbose=True)
print(classifier.evals_result())

Example code for the DNN model:

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(dimensionLen, 1)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(num_class, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
              metrics=['accuracy'], run_eagerly=True)
model.fit(XGBTData, labels, epochs=10)

Example code for true multi‑label training:

mlb = MultiLabelBinarizer()
labels = mlb.fit_transform(cvsData['md5_value'])
classif = OneVsRestClassifier(SVC(kernel='linear'))
classif.fit(XGBTData, labels)

Model Prediction

Prediction follows the same preprocessing steps on new data, ensuring that feature values align with those seen during training (unseen features are set to zero). Predictions from each classifier produce probability vectors for each code snippet, which are then aggregated per original caseId, averaged, and sorted to obtain the final recommendation order.

Example code for XGBoost prediction:

test_y = model.predict_proba(textXGBTData)

Example code for DNN prediction:

predictions = model.predict(TextXGBTData)

Example code for multi‑label prediction:

predictions = classif.predict(TextXGBTData)

The final output is a ranked list of test cases with their recommended script execution priorities, visualized in the accompanying result screenshots.

machine learningfeature engineeringdeep learningautomationmodel trainingXGBoostAPK testing
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.